[英]Copy values from one column to another if values equal to a certain number
[英]Copy Certain substrings from one column to another?
我有一列多行文字。 例如:
「周六早上真好」
“昨天是星期五”
“他晚上去商店了”
如何從每行中提取某些關鍵字,例如星期幾(“星期一”,“星期二”,“星期三”等)並將其存儲在新列中。 我寧願不循環。 也許使用lambda函數?
無需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
您可以為此使用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.