【发布时间】:2022-12-05 00:05:19
【问题描述】:
我想创建一个名为“season_new”的新列,我想在其中维护非空季节并从节目名称中提取空值的季节。我的数据框是这样的:
| programme | season |
|---|---|
| grey's anatomy s1 | null |
| friends season 1 | 1 |
| grey's anatomy s2 | null |
| big bang theory s2 | 2 |
| big bang theory | 1 |
| peaky blinders | 1 |
我会尝试使用正则表达式。
dt['season_new'] = dt['programme'].str.extract(r'(season\s?\d+|s\s?\d+)')
但它给了我这个结果:
| programme | season | season_new |
|---|---|---|
| grey's anatomy s1 | null | 1 |
| friends season 1 | 1 | 1 |
| grey's anatomy s2 | null | 2 |
| big bang theory s2 | 2 | 2 |
| big bang theory | 1 | null |
| peaky blinders | 1 | null |
我期望的结果是:
| programme | season | season_new |
|---|---|---|
| grey's anatomy s1 | null | 1 |
| friends season 1 | 1 | 1 |
| grey's anatomy s2 | null | 2 |
| big bang theory s2 | 2 | 2 |
| big bang theory | 1 | 1 |
| peaky blinders | 1 | 1 |
【问题讨论】: