简体   繁体   中英

Compare values of one column of dataframe in another dataframe

I have 2 dataframes. df1 is

   DATE
2020-05-20
2020-05-21

and df2 is

ID    NAME    DATE
1     abc     2020-05-20
2     bcd     2020-05-20
3     ggg     2020-05-25
4     jhg     2020-05-26

I want to compare the values of df1 with df2, for eg: taking first value of df1 ie 2020-05-20 and find it in df2 and filter it and show output and subset the filtered rows.
My code is

for index,row in df1.iterrows():
    x = row['DATE']
    if x == df2['DATE']:
        print('Found')
        new = df2[df2['DATE'] == x]
        print(new)
    else:
        print('Not Found')

But I am getting the following error:

ValueError: The truth value of a series is ambigious. Use a.empty,a.bool(),a.item(),a.any()

x == df2['DATE'] is a pd.Series (of Booleans), not a single value. You have to reduce that to a single Boolean value in order to evaluate that in a condition.

You can either use .any() or .all() depeding on what you need. I assumed you need .any() here.

for index,row in df1.iterrows():
    x = row['DATE']
    if (x == df2['DATE']).any():
        print('Found')
        new = df2[df2['DATE'] == x]
        print(new)
    else:
        print('Not Found')

Also see here for a pure pandas solution for this.

you can create one extra column in df1 and use np.where to fill it.

import numpy as np
df1['Match'] = np.where(df1.DATE.isin(df2.DATE),'Found', 'Not Found')

this can also be done as a merge which I think makes it a bit clearer as it's only one line with no branching. You can also add the validate parameter to make sure that each key is unique in either the left of right dataset,

import pandas

df1 = pandas.DataFrame(['2020-05-20', '2020-05-21'], columns=['DATE'])
df2 = pandas.DataFrame({'Name': ['abc', 'bcd', 'ggg', 'jgh'], 
                        'DATE': ['2020-05-20', '2020-05-20', '2020-05-25', '2020-05-26']})

df3 = df1.merge(right=df2, on='DATE', how='left')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM