您可以使用split http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.split.html带参数expand=True
并添加一个[]
到左侧:
df[['country','code','com']] = df.A.str.split(',', expand=True)
Then replace http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.replace.html ,
to .
:
df.A = df.A.str.replace(',','.')
print (df)
A B country code com
0 US.65.AMAZON 2016 US 65 AMAZON
1 US.65.EBAY 2016 US 65 EBAY
另一种解决方案是DataFrame
构造函数,如果没有NaN
values:
df[['country','code','com']] = pd.DataFrame([ x.split(',') for x in df['A'].tolist() ])
df.A = df.A.str.replace(',','.')
print (df)
A B country code com
0 US.65.AMAZON 2016 US 65 AMAZON
1 US.65.EBAY 2016 US 65 EBAY
您也可以在构造函数中使用列名称,但是然后concat http://pandas.pydata.org/pandas-docs/stable/generated/pandas.concat.html是必要的:
df1=pd.DataFrame([x.split(',') for x in df['A'].tolist()],columns= ['country','code','com'])
df.A = df.A.str.replace(',','.')
df = pd.concat([df, df1], axis=1)
print (df)
A B country code com
0 US.65.AMAZON 2016 US 65 AMAZON
1 US.65.EBAY 2016 US 65 EBAY