I am a python beginner and I have this issue. I define the following two datasets.
df1 = pd.DataFrame(np.array([10, 20, 30]),
columns=['number1'])
df1
number1
0 10
1 20
2 30
df2 = pd.DataFrame(np.array([[5, 6, 10, 12, 20, 25, 30, 50], [12.3, 15.5, 17.8, 19.0, 25, 18, 18.4, 15.8], [15.3, 18.5, 17.8, 11.0, 28, 17, 19.5, 11.1]]),
index=['number1', 'start', 'end']).T
df2
number1 start end
0 5.0 12.3 15.3
1 6.0 15.5 18.5
2 10.0 17.8 17.8
3 12.0 19.0 11.0
4 20.0 25.0 28.0
5 25.0 18.0 17.0
6 30.0 18.4 19.5
7 50.0 15.8 11.1
What I would like to do is to add start
and end
columns to df1
dataframe containing the corresponding elements of df2
. (if and only if number1==number1). The expected output is the following:
number1 start end
0 10.0 17.8 17.8
1 20.0 25.0 28.0
2 30.0 18.4 19.5
I appended two (2) empty columns to the df1
.
Please note that two datasets are unequal. I followed this solution. However I got this warning.
def equal(df1, df2, ignore_index=True):
if df1['number1'].values == df2['number1'].values:
df1['start'].values = df2['start'].values
df1['end'].values = df2['end'].values
<ipython-input-47-7137be14ffba>:2: DeprecationWarning: elementwise comparison failed; this will raise an error in the future.
if df1['number1'].values == df2['number1'].values:
Do you have any suggestions?
Use df.merge()
:
In [240]: res = df1.merge(df2, on='number1')
In [241]: res
Out[241]:
number1 start end
0 10 17.8 17.8
1 20 25.0 28.0
2 30 18.4 19.5