如何比较 pandas 中两个数据框的值? [英] How to Compare Values of two Dataframes in Pandas?
本文介绍了如何比较 pandas 中两个数据框的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个这样的数据框df
和df2
I have two dataframes df
and df2
like this
id initials
0 100 J
1 200 S
2 300 Y
name initials
0 John J
1 Smith S
2 Nathan N
我想比较(df
和df2
)中找到的initials
列中的值,并将其初始名称与初始名称匹配的名称(在df2
中)复制到第一个数据帧中的初始名称(df
)
I want to compare the values in the initials
columns found in (df
and df2
) and copy the name (in df2
) which its initial is matching to the initial in the first dataframe (df
)
import pandas as pd
for i in df.initials:
for j in df2.initials:
if i == j:
# copy the name value of this particular initial to df
输出应如下所示:
id name
0 100 Johon
1 200 Smith
2 300
有什么办法解决这个问题吗?
Any idea how to solve this problem?
推荐答案
如何?
df3 = df.merge(df2,on='initials',
how='outer').drop(['initials'],axis=1).dropna(subset=['id'])
>>> df3
id name
0 100.0 John
1 200.0 Smith
2 300.0 NaN
因此,"initials"列将被删除,"id"列中带有np.nan
的所有内容也将被删除.
So the 'initials' column is dropped and so is anything with np.nan
in the 'id' column.
如果您不希望在.fillna()
上添加np.nan
:
If you don't want the np.nan
in there tack on a .fillna()
:
df3 = df.merge(df2,on='initials',
how='outer').drop(['initials'],axis=1).dropna(subset=['id']).fillna('')
>>> df3
id name
0 100.0 John
1 200.0 Smith
2 300.0
这篇关于如何比较 pandas 中两个数据框的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文