I have a dataframe which has columns: ch_name and values (separate columns) and for the index is datetime. I want to make like: ch_name must be column name and values must be in the data frame
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
Use 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
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')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.