Reputation: 1592
I have two databses:
Database one:
one has data about name of observation and different measurments that were taken for each obseravtion , e.g:
and database2 which contain the same name observations (but not all of them) and carbom measurment for each.
I want to do these steps: - add empty column in database 1 - if the name in databse2 is in databse 1 , I want to take the carbon value and add it to the new column.
if not, leave it empty.
I have tried to write something but it's really the beginning and I feel stuck :
NaN = np.nan
df['carbon'] = NaN
for i in df.loc['name']:
if i in df_chemo.loc['sample name'] is in df.loc['name']:
I know it is just the beginning but I feel like I don't know how to write what I want.
My end goal: to add to databse 1 new column that will have values from database2 only if the names are match.
Upvotes: 0
Views: 35
Reputation: 945
What you are looking for is the merge
method:
df = df.merge(df_chemo, how='left', on='name')
Example:
import pandas as pd
df1 = pd.DataFrame({'x':[1,2,1,4], 'y':[11,22,33,44]})
print(df1, end='\n ------------- \n')
df2 = pd.DataFrame({'x':[1,2,5,7], 'z':list('abcd')})
print(df2, end='\n ------------- \n')
print(df1.merge(df2, on='x', how='left'))
Output:
x y
0 1 11
1 2 22
2 1 33
3 4 44
-------------
x z
0 1 a
1 2 b
2 5 c
3 7 d
-------------
x y z
0 1 11 a
1 2 22 b
2 1 33 a
3 4 44 NaN
Upvotes: 1