简体   繁体   中英

How to check if one column in a dataframe is exactly equal to a column in another dataframe

I have multiple dataframes with different data but all have a date column. I need to make sure each dataframe's date column exactly matches(data/row etc) something like:

if df1['Date'] == df2['Date'] == df3['Date']:

I cannot for the life of me figure this out.

I was thinking just comparing them and producing a true/false and checking that

(np.where(df1['Date'] == df2['Date'], 'True', 'False')

but this seems inefficient.

any help is appreciated.

Thanks in advance.

EDIT: Shubham pointed out to use

df1['Date'] == df2['Date']

however this produces an error

if df1['Date'] == df2['Date']:

The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I looked into it and when using and/or you can just change to &/|to avoid error but I do not know how to make this if statement work.

Thanks

for the error you are getting use .all()

if (df1['Date'] == df2['Date']).all():

Using np.where is fine, you can also use isin: df1['Date'].isin(df2['Date']) and with value_counts check if there was any date that did not match

False not in df1['Date'].isin(df2['Date']).value_counts()

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