Python 从多行中读取值,并将它们合并到dataframe中的另一行中
我有以下数据帧:Python 从多行中读取值,并将它们合并到dataframe中的另一行中,python,pandas,Python,Pandas,我有以下数据帧: item_id bytes value_id value 1 0 2.0 year 2017 2 0 1.0 month 04 3 0 1.0 day 12 4 0 1.0 time 07 5 0
item_id bytes value_id value
1 0 2.0 year 2017
2 0 1.0 month 04
3 0 1.0 day 12
4 0 1.0 time 07
5 0 1.0 minute 13
6 1 2.0 year 2017
7 1 1.0 month 12
8 1 1.0 day 19
9 1 1.0 time 09
10 1 1.0 minute 32
11 2 2.0 year 2017
12 2 1.0 month 04
13 2 1.0 day 17
14 2 1.0 time 14
15 2 1.0 minute 24
我希望能够计算每个项目的时间\u id
。我如何使用group by here或其他工具来实现以下目标
item_id time
0 2017/04/12 07:13
1 2017/12/19 09:32
2 2017/04/17 14:24
使用
pivot
+到\u datetime
pd.to_datetime(
df.drop('bytes', 1)
.pivot('item_id', 'value_id', 'value')
.rename(columns={'time' :'hour'})
).reset_index(name='time')
item_id time
0 0 2017-04-12 07:13:00
1 1 2017-12-19 09:32:00
2 2 2017-04-17 14:24:00
在进行数据透视之前,您可以删除
字节
列,看起来您并不需要它。使用数据透视
+来查看日期时间
pd.to_datetime(
df.drop('bytes', 1)
.pivot('item_id', 'value_id', 'value')
.rename(columns={'time' :'hour'})
).reset_index(name='time')
item_id time
0 0 2017-04-12 07:13:00
1 1 2017-12-19 09:32:00
2 2 2017-04-17 14:24:00
您可以在旋转前删除
字节
列,看起来您并不需要它。设置索引
+取消堆栈
此外,pd。要想传递数据帧,只需正确命名列即可
pd.to_datetime(df.set_index(['item_id','value_id']).value.unstack().rename(columns={'time' :'hour'}))
Out[537]:
item_id
0 2017-04-12 07:13:00
1 2017-12-19 09:32:00
2 2017-04-17 14:24:00
dtype: datetime64[ns]
set_index
+unstack
此外,pd.to\u datatime
可以传递数据帧,您只需正确命名列即可
pd.to_datetime(df.set_index(['item_id','value_id']).value.unstack().rename(columns={'time' :'hour'}))
Out[537]:
item_id
0 2017-04-12 07:13:00
1 2017-12-19 09:32:00
2 2017-04-17 14:24:00
dtype: datetime64[ns]
优雅是最好的@奥拉克会投票支持你的问题,但我没票了。干杯,优雅是最好的@奥拉克会投票支持你的问题,但我没票了。干杯