我有一个这样的df:
id | authors
1 | smith, john; cameron, james;
2 | guan, brian;
3 | obs, noah; mumm, erik; lee, matt;并希望将其拆分为:
id | author1 | author 2 | author 3
1 | smith, john | cameron, james|
2 | guan, brian | |
3 | obs, noah | mumm, erik | lee, matt我知道pd.split()会根据分隔符一分为二,但这很棘手,因为有些栏目会有1个作者,有些栏目有2个作者,有些栏目会有更多作者。
发布于 2020-11-11 06:56:32
看起来您可以使用带有expand选项的str.split:
df[['id']].join(df.authors.str.strip(';\s*').str.split('; ',expand=True))输出:
id 0 1 2
0 1 mith, john cameron, jame None
1 2 guan, brian None None
2 3 obs, noah mumm, erik lee, matt发布于 2020-11-11 07:00:22
使用str.split和concat函数:
df = pd.concat([df[['id']],df['authors'].str[0:-1].str.split('; ',expand=True)],axis=1)
df.columns = ['id','author1','author2','author3']https://stackoverflow.com/questions/64777969
复制相似问题