简体   繁体   English

将某些子字符串从一列复制到另一列?

[英]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.

相关问题 如果值等于某个数字,则将值从一列复制到另一列 - Copy values from one column to another if values equal to a certain number 将条件值从一列复制到另一列 - Copy conditional values from one column to another 将值从一个 dataframe 列复制到另一列 - Copy values from one dataframe column to another Pandas:从一列的子字符串中提取首字母缩写词并将其与具有条件的另一列匹配 - Pandas: Extract acronym from substrings of one column and match it to another column with a condition Pandas 只从另一列复制值直到某个日期 - Pandas copy values from another column only upto a certain date 如何在 python 中将文本的某一部分从一个文件复制到另一个文件 - How to copy a certain part of text from one file to another in python 使用python将某些文件复制到另一个文件夹 - Copy certain files from one folder to another using python 根据另一列的值复制一列的值 - Copy value from one column based on the value of another column 在python中使用哪种文件shutil复制方法将某些文件从一个文件夹复制到另一个文件夹? - Which file shutil copy method to use in python to copy certain files from one folder to another? 根据 Pandas 中的 id 将列值从一个数据帧复制到另一个数据帧 - Copy column value from one dataframe to another based on id in Pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM