我正在尝试将全名拆分为 pandas 中的第一个中间名和姓氏,但我被困在替换

i am trying to split a full name to first middle and last name in pandas but i am stuck at replace

我试图将名字分成两部分并保留名字和姓氏,最后替换所有部分中的公共部分,这样名字必须是姓氏,然后如果保留中间名,则将其添加到列中

df['owner1_first_name'] = df['owner1_name'].str.split().str[0].astype(str, 
errors='ignore')
df['owner1_last_name'] = 
df['owner1_name'].str.split().str[-1].str.replace(df['owner1_first_name'], 
"").astype(str, errors='ignore')
['owner1_middle_name'] = 
df['owner1_name'].str.replace(df['owner1_first_name'], 
"").str.replace(df['owner1_last_name'], "").astype(str, errors='ignore')

问题是我无法使用 .str.replace(df['owner1_name'], "")
因为我收到错误 "TypeError: 'Series' objects are mutable, thus they cannot be hashed"

pandas 中是否有任何替代语法来实现我想要实现的目标

我想要的输出是

全名 = THOMAS MARY D,在 owner1_name

栏中

我要

owner1_first_name = THOMAS
owner1_middle_name = MARY
owner1_last_name = D

只需更改您的分配并使用另一个变量:

split = df['owner1_name'].split()
df['owner1_first_name'] = split[0]
df['owner1_middle_name'] = split[-1]
df['owner1_last_name'] = split[1]

我认为您需要 mask 将两列中的相同值替换为空字符串:

df = pd.DataFrame({'owner1_name':['THOMAS MARY D', 'JOE Long', 'MARY Small']})

splitted = df['owner1_name'].str.split()
df['owner1_first_name'] = splitted.str[0]
df['owner1_last_name'] = splitted.str[-1]
df['owner1_middle_name'] = splitted.str[1]
df['owner1_middle_name'] = df['owner1_middle_name']
                             .mask(df['owner1_middle_name'] == df['owner1_last_name'], '')
print (df)
     owner1_name owner1_first_name owner1_last_name owner1_middle_name
0  THOMAS MARY D            THOMAS                D               MARY
1       JOE Long               JOE             Long                   
2     MARY Small              MARY            Small  

什么相同:

splitted = df['owner1_name'].str.split()
df['owner1_first_name'] = splitted.str[0]
df['owner1_last_name'] = splitted.str[-1]
middle = splitted.str[1] 
df['owner1_middle_name'] = middle.mask(middle == df['owner1_last_name'], '')
print (df)
     owner1_name owner1_first_name owner1_last_name owner1_middle_name
0  THOMAS MARY D            THOMAS                D               MARY
1       JOE Long               JOE             Long                   
2     MARY Small              MARY            Small                   

编辑:

对于 replace 行可以使用 applyaxis=1:

df = pd.DataFrame({'owner1_name':['THOMAS MARY-THOMAS', 'JOE LongJOE', 'MARY Small']})

splitted = df['owner1_name'].str.split()
df['a'] = splitted.str[0]
df['b'] = splitted.str[-1]

df['c'] = df.apply(lambda x: x['b'].replace(x['a'], ''), axis=1)
print (df)
          owner1_name       a            b      c
0  THOMAS MARY-THOMAS  THOMAS  MARY-THOMAS  MARY-
1         JOE LongJOE     JOE      LongJOE   Long
2          MARY Small    MARY        Small  Small

在我的问题中实现我想要的三行代码是

df['owner1_first_name'] = df['owner1_name'].str.split().str[0]
df['owner1_last_name'] = df.apply(lambda x: x['owner1_name'].split()
[-1].replace(x['owner1_first_name'], ''), axis=1)
df['owner1_middle_name'] = df.apply(lambda x: 
x['owner1_name'].replace(x['owner1_first_name'], 
'').replace(x['owner1_last_name'], ''), axis=1)
splitted = df['Contact_Name'].str.split()
df['First_Name'] = splitted.str[0]
df['Last_Name'] = splitted.str[-1]
df['Middle_Name'] = df['Contact_Name'].loc[df['Contact_Name'].str.split().str.len() == 3].str.split(expand=True)[1]

这可能会有所帮助!这里的部分是正确地插入中间名,你可以通过这段代码来做..