excel - 如何在数据框中的列之间插入新列
问题描述
我需要在数据框中的列之间插入多个新列
输入数据框:
PC GEO BL RL JanTOTAL BL RL FebTOTAL
A USA 1 1 2 1 1 2
B IND 1 1 2 1 1 2
预期输出数据帧
PC GEO Jan-Month BL RL JanTOTAL Feb-Month BL RL FebTOTAL
A USA 2019-01-01 1 1 2 2019-02-01 1 1 2
B IND 2019-01-01 1 1 2 2019-02-01 1 1 2
解决方案
您可以按照以下示例进行操作:
import pandas as pd
from datetime import datetime
df=pd.DataFrame()
df['a']=[1]
df['b']=[2]
print(df)
df['Jan-Month']=datetime(2019,1,1)
df['Feb-Month']=datetime(2019,2,1)
print(df)
df=df.reindex(columns=['Jan-Month','a','Feb-Month','b'])
print(df)
输出:
a b
0 1 2
a b Jan-Month Feb-Month
0 1 2 2019-01-01 2019-02-01
Jan-Month a Feb-Month b
0 2019-01-01 1 2019-02-01 2
推荐阅读
- node.js - 如何使用 nodeJS 从标准输入读取文件
- html - 填充、边距和背景颜色使移动版本未优化?
- spring - 将 mongodb shell 命令转换为 java 代码
- c++ - 从“if constexpr”分支扩展对象生命周期/范围
- microsoft-graph-api - 应用程序能否动态请求比静态授予的权限更多的权限?
- vba - 如何从 Access 中的对象中获取准确的 LastUpdated 日期/时间?
- c# - 如何诊断崩溃的 UWP 应用程序中的故障
- c++ - 如何在其聚合初始化程序中使用类静态对象的大小?
- html - 移动下划线在标题下方并位于左侧
- c++ - Clang 和 MSVC 的模板模板参数推导失败,但 GCC 成功