Ariful Haque
Ariful Haque

Reputation: 163

find a record across multiple python pandas dataframes

Let's say, I have three dataframes as follows, and I would like to find in which dataframes a particular record exists.

this is dataframe1 (df1)

index | name | acct_no | country
2 | alex | 112233 | USA
3 | rider | 223344 | Mexico

This is dataframe2 (df2)

index | name | acct_no | country
2 | alex | 112233 | USA
3 | keith | 993344 | Brazil

This is dataframe3 (df3)

index | name | acct_no | country
2 | alex | 112233 | USA
3 | hopper | 444444 | Canada

So, if I run the following code, I can find all the information about acct_no 112233 for a single dataframe.

p = df1.loc[df1['acct_no']=112233]

But, I would like to know which code will help me find out that acct_no 112233 exists in df1, df2, df3

Upvotes: 1

Views: 194

Answers (1)

Mabel Villalba
Mabel Villalba

Reputation: 2598

One wat to know if the element is in the column 'acct_no' of the dataframe is:

>> (df1['acct_no']==112233).any()
True

You could check all at the same time by doing:

>> all([(df['acct_no']==112233).any() for df in [df1, df2, df3]])

True

Upvotes: 4

Related Questions