pandas 基于拆分另一列添加新列 [英] Pandas add new columns based on splitting another column
本文介绍了 pandas 基于拆分另一列添加新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个如下所示的pandas数据框:
I have a pandas dataframe like the following:
A B
US,65,AMAZON 2016
US,65,EBAY 2016
我的目标是变得像这样:
My goal is to get to look like this:
A B country code com
US.65.AMAZON 2016 US 65 AMAZON
US.65.AMAZON 2016 US 65 EBAY
我知道在此处之前已问过此问题和此处,但其中的没有对我有用.我尝试过:
I know this question has been asked before here and here but none of them works for me. I have tried:
df['country','code','com'] = df.Field.str.split('.')
和
df2 = pd.DataFrame(df.Field.str.split('.').tolist(),columns = ['country','code','com','A','B'])
我错过了什么吗?任何帮助都将不胜感激.
Am I missing something? Any help is much appreciated.
推荐答案
You can use split
with parameter expand=True
and add one []
to left side:
df[['country','code','com']] = df.A.str.split(',', expand=True)
然后 replace
,
至.
:
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
如果没有NaN
值,则使用DataFrame
构造函数的另一种解决方案:
Another solution with DataFrame
constructor if there are no 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
是必需的:
Also you can use column names in constructor, but then concat
is necessary:
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
这篇关于 pandas 基于拆分另一列添加新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文