我的问题是关于re.split()函数中传递的数据。
我有以下数据
|
名称
|
运动
|
|
约翰
|
足球运动NBA,网球
|
|
玛丽
|
壁球、俄罗斯方块;MMA
|
|
斯科特
|
板球、网球
|
|
基姆
|
橄榄球,WNBA;Footy
|
我正试图使用“;”来拆分字符串和','作为分隔符。最初,“名称和运动”列的数据类型为“对象”
import numpy as np
import pandas as pd
import re
df = pd.read_excel(r'Filepath\sports.xlsx',sheet_name = 'data')
df[['Name','Sport']] = df[['Name','Sport']].astype('string')
print(df.dtypes)
df[['A']] = re.split(r';,',df['Sport'])
df
转换为字符串后,然后尝试拆分。我得到以下错误。
TypeError: expected string or bytes-like object
我试着使用
df[['A']] = re.split(r';,',df['Sport'].astype('string'))
但错误一直存在。有什么建议吗?