python - 将熊猫数据框中的列移动到大型数据框中的最后一列的最佳方法
问题描述
我有一个超过 100 列的熊猫数据框。例如在下面的df中:
df['A','B','C','D','E','date','G','H','F','I']
如何将日期移动到最后一列?假设数据框很大,我无法手动编写所有列名。
解决方案
You can try this:
new_cols = [col for col in df.columns if col != 'date'] + ['date']
df = df[new_cols]
Test data:
cols = ['A','B','C','D','E','date','G','H','F','I']
df = pd.DataFrame([np.arange(len(cols))],
columns=cols)
print(df)
# A B C D E date G H F I
# 0 0 1 2 3 4 5 6 7 8 9
Output of the code:
A B C D E G H F I date
0 0 1 2 3 4 6 7 8 9 5
推荐阅读
- java - 必需的字符串
- ios - 以编程方式创建 UICollectionView(在 UIViewController 之外)
- angular - 无需指定长命令选项即可构建我的 Angular 项目
- php - 警告:shell_exec() 出于安全原因已被禁用 - 已从 php.ini 中删除
- django - Django 翻译无法用于生产
- bash - 使用 shell 脚本调用 ROS 启动文件
- java - how to improve performance of multi activity app?
- ios - XCTest 中的屏幕比例因子
- javascript - 使用 Visual Studio Code 处理 ES6 linting
- swift - 使用搜索栏和大标题导航栏拉刷新问题