I have a pandas
dateframe like the following, with DATETIME
as index:
ID Val1 Val2
DATETIME
2019-01-18 10:35:00 A 482.84387 439.67942
2019-01-18 10:35:00 B -5.30216 20.22247
2019-01-18 10:40:00 A -790.63989 -810.00000
2019-01-18 10:40:00 B 257.00000 270.55490
2019-01-18 10:45:00 A 10.54820 5.64659
2019-01-18 10:45:00 B -85.50000 -89.00000
Note that the DATETIME
is repeated for ID
s.
My goal is to convert it to something like the following (with column names changed based on ID, if possible):
A_Val1 A_Val2 B_Val1 B_Val2
DATETIME
2019-01-18 10:35:00 482.84387 439.67942 -5.30216 20.22247
2019-01-18 10:40:00 -790.63989 -810.00000 257.00000 270.55490
2019-01-18 10:45:00 10.54820 5.64659 -85.50000 -89.00000
I used pandas.pivot
but it didn't work.
df_2= df_1.pivot(index=df_1.index, columns='ID', values=['Val1', 'Val2'])
error is:
"DatetimeIndex(['2019-01-18 10:35:00', '2019-01-18 10:35:00',\n ....],\n dtype='datetime64[ns]', name='DATETIME', freq=None) not in index"
I'm not sure where to go from there. Thanks in advance if you can help.
Use DataFrame.reset_index
, pass DATETIME
to index
parameter and last flatten MultiIndex
by f-string
s:
df_2= df_1.reset_index().pivot(index='DATETIME', columns='ID', values=['Val1', 'Val2'])
df_2.columns = df_2.columns.map(lambda x: f'{x[1]}_{x[0]}')
print (df_2)
A_Val1 B_Val1 A_Val2 B_Val2
DATETIME
2019-01-18 10:35:00 482.84387 -5.30216 439.67942 20.22247
2019-01-18 10:40:00 -790.63989 257.00000 -810.00000 270.55490
2019-01-18 10:45:00 10.54820 -85.50000 5.64659 -89.00000
Another way using DataFrame.set_index
and DataFrame.unstack
:
df1 = df.set_index('ID', append=True).unstack()
df1.columns = df1.columns.map(lambda c: f'{c[1]}_{c[0]}')
Result:
# print(df1)
A_Val1 B_Val1 A_Val2 B_Val2
DATETIME
2019-01-18 10:35:00 482.84387 -5.30216 439.67942 20.22247
2019-01-18 10:40:00 -790.63989 257.00000 -810.00000 270.55490
2019-01-18 10:45:00 10.54820 -85.50000 5.64659 -89.00000
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.