在 MATLAB 中,要交换表 A
的第一列和第二列,可以这样做1
A = A(:, [2 1 3:end]);
如果 A
是 pandas DataFrame
是否有类似的便捷方法?
1 MATLAB 使用基于 1 的索引。
最佳答案
pandas 有 reindex 方法可以做到这一点。您只需要按照您希望的顺序提供包含列名的列表:
columns_titles = ["B","A"]
df=df.reindex(columns=columns_titles)
干杯
关于python - 如何交换两个 DataFrame 列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25649429/