df:
home_score away_score
1 0.0 1.0
2 2.0 1.0
3 3.0 2.0
4 0.0 0.0
5 1.0 1.0
预期结果:
score
-- -------
0 0:1
1 2:1
2 2:1
3 3:2
I am trying
df['home_score'] = df['home_score'].astype(str).str.replace('^.*(.d)', '', regex=True)
在组合列之前剥离分数,但我没有得到任何东西..
此外,这段代码在regex101上工作,我如何找出这在python中工作?
先将列转换为整型,去掉十进制部分,然后再将列转换为字符串,然后再连接它们:
cols = ['home_score', 'away_score']
df['score'] = df[cols].astype(int).astype(str).apply(':'.join, axis=1)
print(df)
# Output:
home_score away_score score
1 0.0 1.0 0:1
2 2.0 1.0 2:1
3 3.0 2.0 3:2
4 0.0 0.0 0:0
5 1.0 1.0 1:1