如何更改熊猫数据框结构?

时间:2019-11-05 03:40:07

标签: python pandas

我有一个数据框,其中包含列:ch_name和值(单独的列),索引为datetime。我想这样:ch_name必须是列名,值必须在数据框中

现在的样子:

                                                               ch_name    value
time                                                                           
2019-01-22 00:00:00  Housekeeping.Cardframe_+X_heater-0_Switch_Curr...    0.006
2019-01-22 00:01:00  Housekeeping.Cardframe_+X_heater-0_Switch_Curr...    0.006
2019-01-22 00:02:00  Housekeeping.Cardframe_+X_heater-0_Switch_Curr...    0.006
2019-01-22 00:03:00  Housekeeping.Cardframe_+X_heater-0_Switch_Curr...    0.006
2019-01-22 00:04:00  Housekeeping.Cardframe_+X_heater-0_Switch_Curr...    0.006
...                                                                ...      ...
2019-01-22 23:56:00                             LIN.Lifetime_Cold_Boot  594.000
2019-01-22 23:57:00                             LIN.Lifetime_Cold_Boot  594.000
2019-01-22 23:58:00                             LIN.Lifetime_Cold_Boot  594.000
2019-01-22 23:59:00                             LIN.Lifetime_Cold_Boot  594.000
2019-01-22 23:59:00                             LIN.Lifetime_Cold_Boot  594.000

[239040 rows x 2 columns]

我想变成这样:

                     Housekeeping.Cardframe_+X_heater-0_Switch_Curr    LIN.Lifetime_Cold_Boot    ch_name 3        .... ch_name 166
time                                                                           
2019-01-22 00:00:00      0.006                                                 ....                 values
2019-01-22 00:01:00      0.006                                                 ....
2019-01-22 00:02:00      0.006                                                 ....
2019-01-22 00:03:00      0.006                                                 ....
2019-01-22 00:04:00      0.006                                                 ....
...                                                                
2019-01-22 23:56:00      ....                                                 594.000
2019-01-22 23:57:00      ....                                                 594.000
2019-01-22 23:58:00      ....                                                 594.000
2019-01-22 23:59:00      ....                                                 594.000
2019-01-22 23:59:00 (values have to be saved)                                 594.000

[239040 rows x 166 columns]

注意:有166个频道,但是大熊猫只向我展示了其中2个频道,每天的值都满了

1 个答案:

答案 0 :(得分:0)

您可以像下面那样使用pivot_table

import pandas as pd
from pandas import Timestamp

df = pd.DataFrame([[Timestamp('2019-01-22 00:00:00'), 'Housekeeping.Cardframe_+X_heater-0_Switch_Curr...', 0.006], [Timestamp('2019-01-22 00:01:00'), 'Housekeeping.Cardframe_+X_heater-0_Switch_Curr...', 0.006], [Timestamp('2019-01-22 00:02:00'), 'Housekeeping.Cardframe_+X_heater-0_Switch_Curr...', 0.006], [Timestamp('2019-01-22 00:03:00'), 'Housekeeping.Cardframe_+X_heater-0_Switch_Curr...', 0.006], [Timestamp('2019-01-22 00:04:00'), 'Housekeeping.Cardframe_+X_heater-0_Switch_Curr...', 0.006], [Timestamp('2019-01-22 23:56:00'), 'LIN.Lifetime_Cold_Boot', 594.0], [Timestamp('2019-01-22 23:57:00'), 'LIN.Lifetime_Cold_Boot', 594.0], [Timestamp('2019-01-22 23:58:00'), 'LIN.Lifetime_Cold_Boot', 594.0], [Timestamp('2019-01-22 23:59:00'), 'LIN.Lifetime_Cold_Boot', 594.0], [Timestamp('2019-01-22 23:59:00'), 'LIN.Lifetime_Cold_Boot', 594.0]], columns=('time', 'ch_name', 'value'))
df.set_index("time", inplace=True)

df.pivot_table(values='value', index='time', columns='ch_name')