我有两个数据框:
df1:
A B C
1 A1 B1 C1
2 A2 B2 C2
df2:
B C D
3 B3 C3 D3
4 B4 C4 D4
两者的 B 列和 C 列相同。
我想垂直连接它们并保留第一个 DataFrame 的列:
pd.concat([df1, df2], join_axes=[df1.columns]):
A B C
1 A1 B1 C1
2 A2 B2 C2
3 NaN B3 C3
4 NaN B4 C4
这行得通,但引发了一个
FutureWarning: The join_axes-keyword is deprecated. Use .reindex or .reindex_like on the result to achieve the same functionality.
我找不到(无论是在文档中还是通过 Google)如何“在结果上使用 .reindex 或 .reindex_like 以实现相同的功能”。
说明问题的 Colab 笔记本:https://colab.research.google.com/drive/13EBq2z0Nh05JY7ovrdnLGtfeqdKVvZq0
就像错误提到的添加reindex
pd.concat([df1,df2.reindex(columns=df1.columns)])
Out[286]:
A B C
1 A1 B1 C1
2 A2 B2 C2
3 NaN B3 C3
4 NaN B4 C4
我是一名优秀的程序员,十分优秀!