[英]Modify data frame with new columns
I have the below data frame called df_test that I want to modify and create 4 new columns based on existing values for each id.我有以下名为 df_test 的数据框,我想根据每个 id 的现有值修改并创建 4 个新列。
#df_test
date event id time
17/01/2020 1 596471930 10:18:37
17/01/2020 2 596471930 10:32:48
17/01/2020 3 596471930 12:42:01
17/01/2020 4 596471930 12:44:03
In this example, I want to modify the df_test to display values for all events this way.在这个例子中,我想修改 df_test 以这种方式显示所有事件的值。
date id dateTime event 1 dateTime event 2 dateTime event 3 dateTime event 4
17/01/2020 596471930 17/01/2020 10:18:37 17/01/2020 10:32:48 17/01/2020 12:42:01 17/01/2020 12:44:03
Here is the code for the df_test.这是 df_test 的代码。
import pandas as pd
test = {'date': ['17/01/2020','17/01/2020','17/01/2020','17/01/2020'],
'event': [1,2,3,4],
'id': [596471930,596471930,596471930,596471930],
'time': ['10:18:37','10:32:48','12:42:01','12:44:03']
}
df_test = pd.DataFrame(test, columns = ['date', 'event', 'id', 'time'])
尝试pivot_table
:
pd.pivot_table(df_test, values='time', index=['date', 'id'],columns=['event'],aggfunc='first')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.