问题
I have a pandas dataframe containing (besides other columns) full names:
fullname
martin master
andreas test
I want to create a new column which splits the fullname column along the blank space and assigns the last element to a new column. The result should look like:
fullname lastname
martin master master
andreas test test
I thought it would work like this:
df['lastname'] = df['fullname'].str.split(' ')[-1]
However, I get a KeyError: -1
I use [-1]
, that is the last element of the split group, in order to be sure that I get the real last name. In some cases (e.g. a name like andreas martin master), this helps to get the last name, that is, master.
So how can I do this?
回答1:
You need another str
to access the last splits for every row, what you did was essentially try to index the series using a non-existent label:
In [31]:
df['lastname'] = df['fullname'].str.split().str[-1]
df
Out[31]:
fullname lastname
0 martin master master
1 andreas test test
回答2:
If need create 2 new columns, use str.rsplit with parameter n=1
. If need only last column, EdChum solution is better:
print (df)
fullname
0 martin master
1 andreas test
2 andreas martin master
df[['first_name','last_name']] = df['fullname'].str.rsplit(expand=True, n=1)
print (df)
fullname first_name last_name
0 martin master martin master
1 andreas test andreas test
2 andreas martin master andreas martin master
来源:https://stackoverflow.com/questions/38498718/split-pandas-column-and-add-last-element-to-a-new-column