[英]iterate through the rows of a pandas df to generate new df (depending on conditions)
I have a df with badminton scores. 我有一个羽毛球分数的df。 Each sets of a games for a team are on rows and the score at each point on the columns like so:
团队的每组游戏都在行上,而得分在每一点上都在列上,如下所示:
0 0 1 1 2 3 4 0 0 1 1 2 3 4
0 1 2 3 3 4 4 0 1 2 3 3 4 4
I want to obtain only O and 1 when a point is scored, like so: (to analyse if there any pattern in the points): 得分时,我只想获得O和1,就像这样:(以分析得分中是否有任何模式):
0 0 1 0 1 1 1 0 0 1 0 1 1 1
0 1 1 1 0 1 0 0 1 1 1 0 1 0
I was thinking of using df.itertuples() and iloc and conditions to attribute 1 to new dataframe if next score = score+1 or 0 if next score = score + 1 我正在考虑使用df.itertuples()和iloc和条件将1分配给新数据帧,如果下一个分数=分数+1或0如果下一个分数=分数+ 1
But I dont know how to iterate through the generated tuples and how to generate my new df with the 0 and 1 at the good locations. 但是我不知道如何遍历生成的元组以及如何在合适的位置使用0和1生成新的df。
Hope that is clear thanks for your help. 希望这很感谢您的帮助。 Oh also, any suggestions to analyse the patterns after that ?
哦,还有什么建议可以分析模式呢?
You just need diff
(If you need convert it back try cumsum
) 您只需要
diff
(如果需要将其转换回去,请尝试cumsum
)
df.diff(axis=1).fillna(0).astype(int)
Out[1382]:
1 2 3 4 5 6 7
0 0 0 1 0 1 1 1
1 0 1 1 1 0 1 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.