简体   繁体   English

如何更改 pandas dataframe 结构?

[英]How to change pandas dataframe structure?

I have a dataframe which has columns: ch_name and values (separate columns) and for the index is datetime.我有一个 dataframe 列:ch_name 和值(单独的列),索引是日期时间。 I want to make like: ch_name must be column name and values must be in the data frame我想做这样的事情:ch_name 必须是列名,值必须在数据框中

How it is looks like now:现在的样子:

                                                               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]

I want to be look like:我想看起来像:

                     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]

NOTE: There is 166 channels, but pandas only shows me 2 of them and values are full for each day注意:有 166 个通道,但 pandas 只显示其中 2 个,并且每天的值都是完整的

Use pivot :使用pivot

df = pd.DataFrame({"time":[1,2,3,4],
                   "ch_name":["a","a","b","b"],
                   "value":[0.06,0.06,594.0,594.0]})
df.set_index("time",inplace=True)

print (df)
#     ch_name   value
time                
1          a    0.06
2          a    0.06
3          b  594.00
4          b  594.00
ch_name     a      b

print (pd.pivot(df,columns="ch_name",values="value"))
#
time                
1        0.06    NaN
2        0.06    NaN
3         NaN  594.0
4         NaN  594.0

you can use pivot_table like below您可以使用如下所示的 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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM