我有一个包含(除了其他列)全名的熊猫数据框:
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 列并将最后一个元素添加到新列
发布评论