基于条件拆分Pandas DataFrame列的技术实现问题
处理Pandas DataFrame中标签列的提取问题
问题场景
原始DataFrame如下:
| VideoId | Tags1 | Tags2 |
|---|---|---|
| 1234 | None | None |
| 3456 | Movie:Rocky | Genre:Drama |
| 5678 | Genre:Romance | Movie:Love Me |
| 7890 | Movie:Scream | Genre:Horror |
| 9012 | Movie:Shrek | None |
需求:
- 从
Tags1和Tags2列中,将Movie标题提取到新的Movie列,Genre类型提取到新的Genre列 - 兼容两列中Movie和Genre位置互换的情况
- 遇到
None值时保留None,不触发错误
原有代码存在两个问题:
Movies = df['Tags1'].apply(lambda x: x.split(':')[1] if 'Movie' in x else x) Genres = df['Tags2'].apply(lambda x: x.split(':')[1] if 'Genre' in x else x)
- 处理
None值时会触发索引越界错误(None无split方法) - 无法处理Tags1为Genre、Tags2为Movie的互换场景
期望结果DataFrame:
| VideoId | Movie | Genre |
|---|---|---|
| 1234 | None | None |
| 3456 | Rocky | Drama |
| 5678 | Love Me | Romance |
| 7890 | Scream | Horror |
| 9012 | Shrek | None |
解决方案
通过自定义函数遍历每行标签列,实现兼容None值和位置互换的提取逻辑:
import pandas as pd # 构造原始数据 data = { 'VideoId': [1234, 3456, 5678, 7890, 9012], 'Tags1': [None, 'Movie:Rocky', 'Genre:Romance', 'Movie:Scream', 'Movie:Shrek'], 'Tags2': [None, 'Genre:Drama', 'Movie:Love Me', 'Genre:Horror', None] } df = pd.DataFrame(data) def extract_tags(row): movie = None genre = None # 遍历当前行的两个标签列 for tag in [row['Tags1'], row['Tags2']]: if tag is None: continue if 'Movie:' in tag: movie = tag.split(':')[1] elif 'Genre:' in tag: genre = tag.split(':')[1] return pd.Series([movie, genre], index=['Movie', 'Genre']) # 合并提取结果到原DataFrame result_df = df[['VideoId']].join(df.apply(extract_tags, axis=1)) print(result_df)
逻辑说明
- 函数
extract_tags初始化movie和genre为None,遍历每行的两个标签值 - 遇到
None直接跳过,避免报错 - 根据标签前缀识别类型,提取对应内容,不管标签在
Tags1还是Tags2都能正确匹配 - 最后通过
join将提取出的列与原DataFrame的VideoId列合并,得到目标结果
内容的提问来源于stack exchange,提问作者Aleksei Wolff
相关产品推荐
相关产品推荐

