First of all I am not sure if this is the correct title.
This is my DataFrame:
import pandas as pd
df = pd.DataFrame(
{
'a': [90, 102, 102, 150, 125, 115],
'b': [98, 105, 90, 130, 102, 150],
'label': list('abcdef')
}
)
The expected output is creating column x
:
a b x
0 90 98 NaN
1 102 105 NaN
2 102 90 NaN
3 150 130 NaN
4 125 102 a
5 115 150 c
Basically I want to merge df
with itself. I can get what I want with this code. I don’t know how to put words in order to convey what I want. I splitted df
so that I have something to merge and then create x
.
The splitted dataframes are df1
and df2
.
df1 = df[['a', 'b']]
df2 = df[['a', 'label']].drop_duplicates(subset=['a'], keep='last')
df2['label'] = df2.label.shift(1)
df3 = df1.merge(df2, left_on='b', right_on='a', how='left', suffixes=['', '_y'])
df3 = df3.drop(columns=['a_y'])
df3 = df3.rename(columns={'label': 'x'})
Is there a more convenient way to get the same result?