使用正则表达式用Python中另一列的子字符串填充新列
我有一个df,我想在其中添加一列,从另一列中提取我需要的数据。我从中提取的列包含字符串,因此我猜我需要使用正则表达式或Re来实现这一点 我的df的一个简化示例:使用正则表达式用Python中另一列的子字符串填充新列,python,regex,pandas,Python,Regex,Pandas,我有一个df,我想在其中添加一列,从另一列中提取我需要的数据。我从中提取的列包含字符串,因此我猜我需要使用正则表达式或Re来实现这一点 我的df的一个简化示例: Column A Column B 1 I want (this text) only 2 I only want (this) text 3 th
Column A Column B
1 I want (this text) only
2 I only want (this) text
3 that appears (in) the parentheses
4 but not every line has
5 (parentheses) in it
所以我希望我的df看起来像这样:
Column A Column B Column C
1 I want (this text) only this
2 I only want (this) text this
3 that appears (in) the parentheses in
4 but not every line has
5 (parentheses) in it parentheses
如果只需要括号内的第一个单词,请使用str.extract
,如下所示:
df[“C”]=df[“B”].str.extract(r'\(\S+))
如果需要括号的完整内容,请使用:
df[“C”]=df[“B”].str.extract(r'\(.*?))