简体   繁体   中英

Concatenating two dfs in Pandas

I wanted to know if there was a way to verify if a value in one df that was being Concatenated with another existed in both dfs or the other df as well prior to stacking them on top of each other??

I had two dfs as

df = pd.concat([df1, df2])
df = df.sort_values(by=['id', 'timestamp']).reset_index(drop=True)
df

   id  timestamp
0   1 1959-06-01
1   1 2019-01-01
2   1 2019-01-02
3   2 1989-12-01
4   2 2019-01-15
5   3 1999-01-25
6   3 2019-01-17
7   3 2019-02-01
8   3 2019-02-03

Was there a way to to verify that the ID in df1 existed in df2 before concatenating similar to a merge? I didn't need to merge but instead concatenate the dfs on top of each other.

One df has multiple ids and timestamps and the other had only one ID and I wanted to make sure only IDs that existed in both were in the resulting concatenated df

thanks!

One workaround you can do here is to create a dummy column:

df1["df"] = 1
df2["df"] = 2
df = pd.concat([df1, df2])

That way you can see where each row was derived.

check for entire row

df3=pd.concat([df1,df2[~df2.isin(df1)]],ignore_index=True).dropna()

check for one column

df3=pd.concat([df1,df2[~df2['col_name'].isin(df1['col_name'])]],ignore_index=True).dropna()

is this what you are looking for? attaching a sample code.

df = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3', 'K4', 'K5'],'A': ['A0', 'A1', 'A2', 'A3', 'A4', 'A5']})
other = pd.DataFrame({'key': ['K0', 'K1', 'K2'],'B': ['B0', 'B1', 'B2']})
new = df.set_index('key').join(other.set_index('key'))
new.dropna(axis=0, how='any', thresh=None, subset=None, inplace=True)
print(new)

Or

import pandas as pd 
df = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3', 'K4', 'K5'],'A': ['A0', 'A1', 'A2', 'A3', 'A4', 'A5']})
other = pd.DataFrame({'key': ['K0', 'K1', 'K2'],'A': ['A0', 'A1', 'A2']})
new = pd.merge(df , other , how = 'inner')
print(new)

does this help you?

您可以使用df.isin,请尝试。

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