问题描述
我有一个包含(除了其他列)全名的熊猫数据框:
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]
但是,我得到一个 KeyError: -1
我使用 [-1]
,这是拆分组的最后一个元素,以确保我得到真实的姓氏.在某些情况下(例如像 andreas martin master 这样的名字),这有助于获得姓氏,即 master.
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.
那我该怎么做呢?
推荐答案
您需要另一个 str
来访问每一行的最后一个拆分,您所做的实际上是尝试使用非-存在的标签:
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
这篇关于拆分 pandas 列并将最后一个元素添加到新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!