拆分 pandas 列并将最后一个元素添加到新列

编程入门 行业动态 更新时间:2024-10-28 10:32:23
本文介绍了拆分 pandas 列并将最后一个元素添加到新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个包含(除了其他列)全名的熊猫数据框:

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 列并将最后一个元素添加到新列

本文发布于:2023-10-27 16:22:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1533912.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并将   到新   元素   pandas

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!