[英]Copy Certain substrings from one column to another?
I have a column of text of many rows. 我有一列多行文字。 For instance:
例如:
"Saturday morning was nice" 「周六早上真好」
"Yesterday was Friday" “昨天是星期五”
"He went to the store at night" “他晚上去商店了”
How can I extract certain keywords, say the days of the week ("Monday", "Tuesday", "Wednesday", etc.) from every row and store it in a new column. 如何从每行中提取某些关键字,例如星期几(“星期一”,“星期二”,“星期三”等)并将其存储在新列中。 I'd rather not loop.
我宁愿不循环。 Maybe use a lambda function?
也许使用lambda函数?
No need lambda , here is findall
无需lambda,这是
findall
#l=['Monday',...'Sunday'] define you own list
df['Newcol']=df.Date.str.findall('|'.join(l))
0 [Saturday]
1 [Friday]
2 []
Name: Date, dtype: object
You can use pandas.Series.str.extract
for this: 您可以为此使用
pandas.Series.str.extract
:
words = ['Saturday', 'Friday']
df['Word'] = df['Strings'].str.extract(r'({})'.format('|'.join(words)))
print(df)
Strings Word
0 Saturday morning was nice Saturday
1 Yesterday was Friday Friday
2 He went to the store at night NaN
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.