我有一个数据帧,看起来像这样:
eventScore
5-4
6-2 6-0
6-2 6-3
7-6 6-2
2-6 4-6
1-6 1-3
每对的左边是球员得分,每对的右边是opponentScore。
因此,对于每一列,我需要添加破折号的左侧和右侧。
最终输出:
eventScore playerScore opponentScore
5-4 5 4
6-2 6-0 12 2
6-2 6-3 12 5
7-6 6-2 13 8
2-6 4-6 6 12
1-6 1-3 2 9
数据可以根据需要重新格式化。
编辑:原始问题在分数中有不必要的字符串,使其更容易阅读。
发布于 2018-09-20 04:34:02
虽然不是特别优雅,但它很有效,使用正则表达式查找-
前后的数字
df['playerScore'] = df.eventScore.str.findall('(\d+)-').apply(pd.Series, dtype=float).sum(1)
df['opponentScore'] = df.eventScore.str.findall('-(\d+)').apply(pd.Series, dtype=float).sum(1)
>>> df
eventScore playerScore opponentScore
0 5-4 5.0 4.0
1 6-2 6-0 12.0 2.0
2 6-2 6-3 12.0 5.0
3 7-6 6-2 13.0 8.0
4 2-6 4-6 6.0 12.0
5 1-6 1-3 2.0 9.0
https://stackoverflow.com/questions/52413581
复制相似问题