使用str.split函数在数据框中拆分列 [英] Splitting a column in dataframe using str.split function

查看:174
本文介绍了使用str.split函数在数据框中拆分列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将用逗号分隔的值的列拆分为2列,但是str.split函数返回的列的值为0和1,而不是拆分的字符串值

I am trying to split a column with comma delimited values into 2 columns but the str.split function returns columns with 0's and 1's instead of the split string values

我有一个带有全名"列的数据框,其中有一个全名,用逗号分隔姓氏和名字.

I have a dataframe with a column 'Full Name' which has a full name with a comma separating last name from first name.

我使用了str.split函数,该函数仅在执行显示时才起作用.但是:当我尝试使用相同的功能将2个新列添加到具有拆分数据的同一数据帧中时,我会得到2个新列,其中第一个填充为0,第二个填充为1.

I used the str.split function which works when I execute it for display only. But: when I try to use the same function to add 2 new columns to the same dataframe with the split data, I get 2 new columns with the first populated with 0's and the second with 1's all the way.

用于显示拆分数据的代码:

The code that works to display the split data:

df2015_2019.iloc[:,0].str.split(',', expand=True)

无法使用拆分数据创建新列的代码:

Code that doesn't work to create new columns with split data:

df2015_2019['Lname'],df2015_2019['Fname'] = df2015_2019.iloc[:,0].str.split(',', expand=True)

我得到一个全为0的列"Lname"和一个全为1的列"Fname"

I get a column 'Lname' with all 0's and a column 'Fname' with all 1's

推荐答案

另一种实现此目标的方法如下.

Another way around to achieve this as follows..

>>> df = pd.DataFrame({'Name': ['Karn,Kumar', 'John,Jimlory']})
>>> df
           Name
0    Karn,Kumar
1  John,Jimlory

结果:

您可以在拆分值时分配列名,如下所示.

Result:

You can assign the column name while splitting the values as below.

>>> df[['First Name','Last Name']] = df['Name'].str.split(",", expand=True)
>>> df
           Name First Name Last Name
0    Karn,Kumar       Karn     Kumar
1  John,Jimlory       John   Jimlory

或者,如另一个回答所述..

Or, as another answer stated..

>>> df['Name'].str.split(",", expand=True).rename({0: 'First_Name', 1: 'Second_Name'}, axis=1)
  First_Name Second_Name
0       Karn       Kumar
1       John     Jimlory

OR

>>> df['Name'].str.rsplit(",", expand=True).rename(columns={0:'Fist_Name', 1:'Last_Name'})
  Fist_Name Last_Name
0      Karn     Kumar
1      John   Jimlory

注意:您可以同时使用axis = columnsaxis =1.

使用 Series.str.partition ,但几乎没有替代,但是我们必须使用drop,因为partition保留逗号和"以及列.

Just another way using Series.str.partition with little altercation, However, we have to use drop as partition preserves the comma "," as well as a column.

>>> df['Name'].str.partition(",", True).rename(columns={0:'Fist_Name', 2:'Last_Name'}).drop(columns =[1])
  Fist_Name Last_Name
0      Karn     Kumar
1      John   Jimlory

只要苗条,我们就可以为rename定义dict值.

Just make it slim, we can define dict values for the rename.

1-使用str.partition ..

dict = {0: 'First_Name', 2: 'Second_Name'}

df = df['Name'].str.partition(",", True).rename(dict2,axis=1).drop(columns =[1])
print(df)

  First_Name Second_Name
0       Karn       Kumar
1       John     Jimlory

2-使用str.split() ..

dict = {0: 'First_Name', 1: 'Second_Name'}

df = df['Name'].str.split(",", expand=True).rename(dict, axis=1)
 print(df)
  First_Name Second_Name
0       Karn       Kumar
1       John     Jimlory

这篇关于使用str.split函数在数据框中拆分列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆