我有一项任务是将所有子dfs合并到Master df。下面是master_df,只有列(多索引)
master_df:
first bar baz foo
second one two thr fou fiv six
所以在master_df中,level = 0列是'first'而level = 1列是'second'。我的任务是为此数据框添加值(追加每一行)
我的data_dfs在
之下data_df1:
one two
a 1.23 2.23
b 3.41 0.23
data_df2:
fiv six
a 3.13 4.25
b 1.42 NaN
注意到data_df1和data_df2具有匹配level = 1的单级列(master_df中的“第二个”列)。我需要将我的两个数据dfs值合并到我的Master_df中。
master_df: #after the merge
first bar baz foo
second one two thr fou fiv six
a 1.23 2.23 NaN NaN 3.13 4.25
b 3.41 0.23 NaN NaN 1.42 NaN
由于master_df中的所有“第二”列(level = 1)都是唯一的,我只需要匹配名称来放入每个值。这是否可以不为每个data_dfs创建多索引列?请帮帮我。谢谢!
答案 0 :(得分:1)
mux = master_df.columns
df = pd.concat([data_df1,data_df2], axis=1).reindex(columns=mux, level=1)
print (df)
first bar baz foo
second one two thr fou fiv six
a 1.23 2.23 NaN NaN 3.13 4.25
b 3.41 0.23 NaN NaN 1.42 NaN
来自master_df
的数据在加入的DataFrames
中不存在,因此需要append
:
print (master_df)
first bar baz foo
second one two thr fou fiv six
0 0 4 5 6 8 7
1 4 5 8 7 1 2
df = (pd.concat([data_df1,data_df2], axis=1).reindex(columns=master_df.columns, level=1)
.append(master_df, ignore_index=True))
print (df)
first bar baz foo
second one two thr fou fiv six
0 1.23 2.23 NaN NaN 3.13 4.25
1 3.41 0.23 NaN NaN 1.42 NaN
2 0.00 4.00 5.0 6.0 8.00 7.00
3 4.00 5.00 8.0 7.0 1.00 2.00