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
我在嘗試
df['home_score'] = df['home_score'].astype(str).str.replace('^.*(.\d)', '', regex=True)
在組合列之前去除分數,但我什么也沒得到..
此外,此代碼適用于regex101我如何弄清楚它在 python 中作業?
uj5u.com熱心網友回復:
首先將您的列轉換為 int 以洗掉小數部分,然后在加入它們之前轉換為字串:
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/336102.html
