pandas 数据重排 [英] Pandas Data Re-arrangement
问题描述
我正在从事体育运动. 目的是记录游戏中的当前eventdatetime和PreviousEventTime.我在下面的链接中有一个示例数据集.
I am working on sport. The purpose is to record current eventdatetime and PreviousEventTime in a game. I have a sample dataset in the below link.
https://drive.google.com/open?id=1DUNrWPFwrkZHpq_KeA4rZCJ94sbpUEDI
在此文件中,有11列.该事件是根据时间收集的. 对于这次重新安排,我将使用以下各列 gsm_ID ,事件日期时间列
In this file, there are 11 columns. the event are collected based on time. For this re-arrange, i will be using the following columns gsm_ID,eventdatetime columns
我想创建一个新列 PreviousEventTime ,该列占用 eventdatetime 列的n-1行. 这意味着对于每个 gsm_ID ,将有第一个 eventdatetime . 与时间列相比,新列将代表下一个事件时间.
I want to create a new column PreviousEventTime that take n-1 row of the eventdatetime column. That means for every gsm_ID, there will be the first eventdatetime. The new column will represent the next event time as compared to the time column.
gsm_ID eventdatetime PreviousEventTime
2462794 08/11/2017 18:46 08/11/2017 18:45
2462794 08/11/2017 18:49 08/11/2017 18:46
2462794 08/11/2017 19:13 08/11/2017 18:49
2462794 08/11/2017 19:31 08/11/2017 19:13
2462794 08/11/2017 20:09 08/11/2017 19:31
2462795 08/12/2017 17:39 08/12/2017 16:30
2462795 08/12/2017 17:44 08/12/2017 17:39
以上示例仅适用于两个游戏.您可以通过 gsm_id 进行区分.上一个事件时间的for行将始终为 matchdatetime.. 我将有100场比赛.但是该过程将重复上述示例.
Above example is just for two games. You can differentiate by gsm_id. The for row at PreviousEventTime will always be matchdatetime. I will have 100 over games. but the process will repeat as above-mentioned example.
您的建议将不胜感激. 问候, 和风
Your advice would be much appreciated. Regards, zephyr
推荐答案
首先,在我们可以使用shift方法之后,必须将数据集加载到pandas数据框中.
First you have to load your dataset into pandas dataframe after we can use shift method.
last_val=df["Time"].iloc[-1]
df['second_eventime']=df['Time'].shift(-1) #This will leave last column value as blank
df.iloc[-1, df.columns.get_loc('second_eventime')] = last_val #To Maintain the value at last row
这篇关于 pandas 数据重排的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!