我有这个df:
hour minute a1_UNf a1_UNf b1_UNf
max max mean amax mean
1min
2017-09-19 02:00:00 2 0 12561.6041666667 12565.5 12560.4821428571
2017-09-19 02:01:00 2 1 12560.0779220779 12562.5 12559.3311688312
2017-09-19 02:02:00 2 2 12558.45 12559.5 12557.7
2017-09-19 02:03:00 2 3 12556.2536231884 12560 12555.4347826087
2017-09-19 02:04:00 2 4 12555.9444444444 12557 12555.3518518519
2017-09-19 02:05:00 2 5 12554.859375 12555.5 12554.1875
2017-09-19 02:06:00 2 6 12552.5 12553.5 12551.8
我正在尝试创建新的标题,这两个标题将是两个顶部标题的组合(由groupby创建)
有关如何制作标题的任何建议
hour_max minute_max
等?
我试图使用:
col_name = df.columns.values
并打印:
[('hour', 'max') ('minute', 'max') ('a1_UNf', 'mean')
('a1_UNf', 'amax') ('b1_UNf', 'mean')]
但是如何将其作为单个标题应用?
答案 0 :(得分:3)
您可以将map
与join
:
df.columns = df.columns.map('_'.join)
#list comprehension alternative
#df.columns = ['{0[0]}_{0[1]}'.format(x) for x in df.columns]
print (df)
hour_max minute_max a1_UNf_mean a1_UNf_amax \
1min
2017-09-19 02:00:00 2 0 12561.604167 12565.5
2017-09-19 02:01:00 2 1 12560.077922 12562.5
2017-09-19 02:02:00 2 2 12558.450000 12559.5
2017-09-19 02:03:00 2 3 12556.253623 12560.0
2017-09-19 02:04:00 2 4 12555.944444 12557.0
2017-09-19 02:05:00 2 5 12554.859375 12555.5
2017-09-19 02:06:00 2 6 12552.500000 12553.5
b1_UNf_mean
1min
2017-09-19 02:00:00 12560.482143
2017-09-19 02:01:00 12559.331169
2017-09-19 02:02:00 12557.700000
2017-09-19 02:03:00 12555.434783
2017-09-19 02:04:00 12555.351852
2017-09-19 02:05:00 12554.187500
2017-09-19 02:06:00 12551.800000