将 DataFrame 合并到另一个包含所有重复值的 DataFrame 中,不同的将被替换为字符串
Merge DataFrame into another dataframe contaning all duplicates values, and the different ones will be replaced by string
我有两个dataframe:df1和df2
Index1=[5,10,15,20]
Index2=[5,10,13,15,20]
d1 = {'a' : pd.Series([1., 6., 3., 2.], index=Index1)}
d2 = {'a' : pd.Series([1., 5., 3., 2.], index=Index1)}
df1=pd.DataFrame(d1)
df2=pd.DataFrame(d2)
a
5 1.0
10 6.0
15 3.0
20 2.0
a
5 1.0
10 5.0
15 3.0
20 2.0
我想将它们合并到另一个数据帧中,并获得如下输出:
a
5 1.0
10 DIFF
15 3.0
20 2.0
非常感谢您的帮助
您可以使用 mask
、astype
和 fillna
:
df1.mask((df1 - df2).astype(bool)).fillna('DIFF')
输出:
a
5 1
10 DIFF
15 3
20 2
我有两个dataframe:df1和df2
Index1=[5,10,15,20]
Index2=[5,10,13,15,20]
d1 = {'a' : pd.Series([1., 6., 3., 2.], index=Index1)}
d2 = {'a' : pd.Series([1., 5., 3., 2.], index=Index1)}
df1=pd.DataFrame(d1)
df2=pd.DataFrame(d2)
a
5 1.0
10 6.0
15 3.0
20 2.0
a
5 1.0
10 5.0
15 3.0
20 2.0
我想将它们合并到另一个数据帧中,并获得如下输出:
a
5 1.0
10 DIFF
15 3.0
20 2.0
非常感谢您的帮助
您可以使用 mask
、astype
和 fillna
:
df1.mask((df1 - df2).astype(bool)).fillna('DIFF')
输出:
a
5 1
10 DIFF
15 3
20 2