英文:
How to get first name and last name when last name is multiple names in pandas
问题
我有一个数据框,需要分离名字的姓和名。到目前为止,我已经做到了这一步。
df = [['Victor De La Cruz', 'Ashley Smith', 'Angel Miguel Hernandez', 'Hank Hill']]
df['first_name'] = df.str.split().str[0]
df['last_name'] = df.str.split().str[1:]
输出结果如下:
first_name last_name
0 Victor [De, La, Cruz]
1 Ashley [Smith]
2 Angel [Miguel, Hernandez]
3 Hank [Hill]
我尝试使用 df['last_name'].replace('[', '')
来去除不需要的所有字符,但没有成功。
期望的输出如下:
first_name last_name
0 Paul De La Cruz
1 Ashley Smith
2 Angel Miguel Hernandez
3 Hank Hill
有任何建议吗?谢谢!
英文:
I have a data frame and need to separate first and last name. So far this is where I got to.
df = [['Victor De La Cruz', 'Ashley Smith', 'Angel Miguel Hernandez', 'Hank Hill']]
df['first_name'] = df.str.split().str[0]
df['last_name'] = df.str.split().str[1:]
OutPut
first_name last_name
Victor [De, La, Cruz]
Ashley [Smith]
Angel [Miguel, Hernandez]
Hank [Hill]
I have tried using df'last_name'].replace('[', '')
for all characters not wanted but it didn't work.
Desired Output
first_name last_name
Paul De La Cruz
Ashley Smith
Angel Miguel Hernandez
Hank Hill
Any Suggestions would be helpful thank you!
答案1
得分: 1
在 split()
后,您的系列中包含列表对象,而不是字符串,这就是为什么 .replace()
没有意义的原因。
英文:
Just join back
df['last_name'] = df['last_name'].str.join(' ')
After the split()
, you have list objects in your series, not strings, which is why .replace()
doesn't make sense.
答案2
得分: 1
I'd suggest using the n
keyword argument to limit the splits to only the first space. You could also use expand=True
:
import pandas as pd
s = pd.Series([
'Victor De La Cruz',
'Ashley Smith',
'Angel Miguel Hernandez',
'Hank Hill'
])
df = s.str.split(n=1, expand=True)
df.columns = ["first_name", "last_name"]
first_name last_name
0 Victor De La Cruz
1 Ashley Smith
2 Angel Miguel Hernandez
3 Hank Hill
英文:
I'd suggest using the n
keyword argument to limit the splits to only the first space. You could also use expand=True
:
import pandas as pd
s = pd.Series([
'Victor De La Cruz',
'Ashley Smith',
'Angel Miguel Hernandez',
'Hank Hill'
])
df = s.str.split(n=1, expand=True)
df.columns = ["first_name", "last_name"]
first_name last_name
0 Victor De La Cruz
1 Ashley Smith
2 Angel Miguel Hernandez
3 Hank Hill
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论