我有这个DataFrame,来自一组相同的数据框,但它们与total_inflow
具有相同的列名,但我需要更改这些名称以指示彼此不同的列作为total_inflow_t1
, total_inflow_t2
,...
所以我有这个:
In [227]:
all = DataFrame([node_t1["total_inflow"], node_t2["total_inflow"], node_t3["total_inflow"], node_t4["total_inflow"], node_t5["total_inflow"]]).T
Out[227]:
total_inflow total_inflow total_inflow total_inflow total_inflow
time
01/01/01 00:01:00 0.0085 0.0040 0.0002 0.0001 0.0001
01/01/01 00:02:00 0.2556 0.1669 0.0590 0.0012 0.0001
01/01/01 00:03:00 0.9935 0.7699 0.3792 0.0283 0.0002
01/01/01 00:04:00 1.3873 1.2879 0.8767 0.1614 0.0011
所以我需要得到这个:
Out[227]:
total_inflow_t1 total_inflow_t2 total_inflow_t3 total_inflow_t4 total_inflow_t5
time
01/01/01 00:01:00 0.0085 0.0040 0.0002 0.0001 0.0001
01/01/01 00:02:00 0.2556 0.1669 0.0590 0.0012 0.0001
01/01/01 00:03:00 0.9935 0.7699 0.3792 0.0283 0.0002
01/01/01 00:04:00 1.3873 1.2879 0.8767 0.1614 0.0011
答案 0 :(得分:1)
这是一个更通用的解决方案。它会为所有列添加后缀。
df.columns = ['{}_t{}'.format(k,i+1) for i,k in enumerate(df.columns)]
答案 1 :(得分:0)
创建数据框all
后,使用以下命令更新列名称:
all.columns = ['total_inflow_t1', 'total_inflow_t2', 'total_inflow_t3',
'total_inflow_t4', 'total_inflow_t5']
答案 2 :(得分:0)
另一种方法是在keys
中使用pd.concat
参数:
all = pd.concat([node_t1["total_inflow"], node_t2["total_inflow"],
node_t3["total_inflow"], node_t4["total_inflow"],
node_t5["total_inflow"]],
axis=1,
keys=['total_inflow_t1', 'total_inflow_t2',
'total_inflow_t3','total_inflow_t4',
'total_inflow_t5']).T