[英]Python pandas - extract and replace
I have a Pandas data frame column containing elements similar to the string McNally, King (XYZ)
. 我有一个Pandas数据框列,其中包含类似于字符串McNally, King (XYZ)
元素。 I would like to keep the last name, first name and remove everything else. 我想保留姓氏,名字并删除其他所有内容。 Therefore after cleaning McNally, King (XYZ)
should be McNally, King
. 因此,在清洁McNally, King (XYZ)
应该是McNally, King
。
I have tried following two functions but not getting the desired result: 我尝试了以下两个功能,但未获得预期的结果:
df['name'] = df['name'].str.extract(r'\w+\,\s[A-Z][a-z]+', expand=False)
df['name'] = df['name'].replace({r'\w+\,\s[A-Z][a-z]+' : r'\w+\,\s[A-Z][a-z]+'}, regex=True)
Second code replaces the substring with the regex itself, while the first code extracts the names from the string but I want to keep the name and remove everything else followed by the name. 第二个代码用正则表达式本身替换子字符串,而第一个代码从字符串中提取名称,但我想保留名称并删除所有其他名称。
Edit: Sample data: 编辑:样本数据:
Reyes, Rebecca L (XYZ)
Derry, Odd P (XYZ)
Garza, Per-Laura A (MNP)
Fernandez, Rafael Carl (XYZ)
Expected output: 预期产量:
Reyes, Rebecca
Derry, Odd
Garza, Per-Laura
Fernandez, Rafael
I would like to edit-in-place ie modify the existing datafame itself and not creating a new one. 我想就地编辑,即修改现有数据名本身而不创建新的数据名。
You can try something like this: 您可以尝试如下操作:
df = pd.DataFrame({'name':['McNally, King (XYZ)']}, index=[0])
df['name'].str.extract(r'(\w+,\s\w+)')
Output: 输出:
0 McNally, King
Name: name, dtype: object
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.