我编写了一个代码,将全名拆分为名字、中间名和姓氏。
import pandas as pd
df= pd.read_excel(r"D:\SAMPLE EXCEL FILES\names.xlsx")
splitted = df['Names'].str.split()
first = splitted.str[0]
middle = splitted[1]
last = splitted.str[-1]
df['First Name'] = first
df['Middle Name'] = middle.mask(middle == last,'')
df['Last Name'] = last
print(df)
我收到以下错误:
Traceback (most recent call last):
File "D:/names.py", line 18, in <module>
df['Middle Name'] = middle.mask(middle == last,'')
AttributeError: 'list' object has no attribute 'mask'
打印顺序很重要:First、Middle 和 Last。
我无法解决这个问题。
没有示例数据,很难提供准确的解决方案,但这可能对您有用。
df[['First Name', 'Middle Name', 'Last Name']] = df['Names'].str.split(' ', expand=True).ffill(axis=1)
df['Middle Name'] = np.where(df['Middle Name'].eq(df['Last Name']), '', df['Middle Name'])