我目前有两个数据帧共享一个公共列,如下所示:
df1 =
Text Val
"This" 1
"That" 2
"Is" 3
"Not" ''
"Working" ''
df2 =
Text Val
"This" ''
"That" ''
"Is" ''
"Not" 4
"Working" 5
我想按以下方式合并两个数据帧:
merged_df =
Text Val
"This" 1
"That" 2
"Is" 3
"Not" 4
"Working" 5
本质上,两个数据帧 df1 和 df2 都共享一个公共的"文本"列。 所有值都是相似的。 但是,在 df1 中,对应于"Not"和"Working"的行没有值,但这些值在 df2 中可用。
如何将它们堆叠在一起以得出merged_df?
与我的实际数据相比,这些示例微不足道,但希望这会有所帮助
您可以将combine_first
与set_index
一起使用,以对齐数据并替换为NaN
秒(如有必要(:
df1['Val'] = df1['Val'].replace("''",np.nan)
df2['Val'] = df2['Val'].replace("''",np.nan)
df = df1.set_index('Text').combine_first(df2.set_index('Text')).reset_index()
print (df)
Text Val
0 This 1
1 That 2
2 Is 3
3 Not 4
4 Working 5