我有一个看起来像这样的数据框:
eventScore
5-4
6-2 6-0
6-2 6-3
7-6 6-2
2-6 4-6
1-6 1-3
每对左边是玩家得分,每对右边是对手得分。
因此,对于每一列,我需要添加短划线的左侧和短划线的右侧。
最终输出:
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
可以根据需要重新格式化数据。
编辑:原始问题的分数中没有多余的字符串,因此更易于阅读。
答案 0 :(得分:2)
不太优雅,但是可以使用正则表达式在-
之前和之后查找数字:
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