我正在尝试清理列:
df:
+-----+------------------+--------------------+--------------------+--------------+--------------+
| | league | home_team | away_team | home_score | away_score |
+=====+==================+====================+====================+==============+==============+
| 0 | Champions League | APOEL | Qarabag | 1 | 2 |
+-----+------------------+--------------------+--------------------+--------------+--------------+
| 1 | Champions League | FC Copenhagen | TNS | 1 | 0 |
+-----+------------------+--------------------+--------------------+--------------+--------------+
| 2 | Champions League | AIK | Maribor | 3 | 2 ET |
+-----+------------------+--------------------+--------------------+--------------+--------------+
预期的
df:
+-----+------------------+--------------------+--------------------+--------------+--------------+
| | league | home_team | away_team | home_score | away_score |
+=====+==================+====================+====================+==============+==============+
| 0 | Champions League | APOEL | Qarabag | 1 | 2 |
+-----+------------------+--------------------+--------------------+--------------+--------------+
| 1 | Champions League | FC Copenhagen | TNS | 1 | 0 |
+-----+------------------+--------------------+--------------------+--------------+--------------+
| 2 | Champions League | AIK | Maribor | 3 | 2 |
+-----+------------------+--------------------+--------------------+--------------+--------------+
我在尝试
df['away_score'] = df['away_score'].astype(str).str.replace('(\s?\w+)$', '', regex=True)
(适用于 regex101 但不适用于 pandas)
但是列中的所有数据都被替换了。
+-----+------------------+--------------------+--------------------+--------------+--------------+
| | league | home_team | away_team | home_score | away_score |
+=====+==================+====================+====================+==============+==============+
| 0 | Champions League | APOEL | Qarabag | 1 | |
+-----+------------------+--------------------+--------------------+--------------+--------------+
| 1 | Champions League | FC Copenhagen | TNS | 1 | |
+-----+------------------+--------------------+--------------------+--------------+--------------+
| 2 | Champions League | AIK | Maribor | 3 | 2 |
+-----+------------------+--------------------+--------------------+--------------+--------------+
什么应该是正确的正则表达式?