[英]Creating a new data frame column, by splitting a string in another column in pandas
I'm trying to break down some data I have in a data frame, that looks like this for example: 我正在尝试分解数据框中的某些数据,例如:
Index Name
0 joe bloggs
1 jane doe
2 first last
I want to create a new column, with just the last name for example. 我想创建一个新列,仅以姓氏为例。
I feel like I should be doing something along the lines of df['New_name'] = df['Name'].split()
but I know that won't work. 我觉得我应该按照
df['New_name'] = df['Name'].split()
但是我知道这行不通。
Any help would be really appreciated, cheers 任何帮助将不胜感激,欢呼
df['New_name'] = df['Name'].str.split(expand =True)[1]
gets the second word. df['New_name'] = df['Name'].str.split(expand =True)[1]
获取第二个单词。 If you want to make sure you get the last word, df['New_name'] = df['Name'].apply(lambda x: x.split()[-1])
works. 如果要确保得到最后一个单词,则可以使用
df['New_name'] = df['Name'].apply(lambda x: x.split()[-1])
。
String handling in pandas series is a little weird. 熊猫系列中的字符串处理有点奇怪。 How about
怎么样
df1['firstname'],df1['lastname']=df1['name'].str.split().str
This should also do the trick: 这也应该达到目的:
df['Name'].str.split(' ').str[-1]
0 bloggs
1 doe
2 last
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.