简体   繁体   English

如果在特定符号上匹配,如何将数据框中的字符串搜索并提取到新列中?

[英]How to Search and Extract string in dataframe into new column if match on particular symbol?

I have a df that has non readable alpha-numerical values for column names. 我有一个df,它的列名具有不可读的字母数字值。

I want to extract entire email addresses from any column (cell) that has the @ symbol in it. 我想从其中带有@符号的任何列(单元格)中提取整个电子邮件地址。 How would I search the entire dataframe regardless of column for the @ symbol? 我如何在整个数据帧中搜索@符号,而不管其列如何?

Input df:
    a.1   b.2        c.1      
    aa    a@a.com    12        
    bb    b@b.com    29       
    cc              
    dd    d@d.com              

example df I want post extraction: 我想要后期提取的示例df:

a.1   b.2        c.1       email
aa    a@a.com    12        a@a.com
bb    b@b.com    29        b@b.com
cc              
dd    d@d.com              d@d.com

Using str.contains 使用str.contains

df['email']=df.loc[df['b.2'].str.contains('@'),'b.2']

Seems like you need 好像你需要

df['email']=df[df.apply(lambda x : x.str.contains('@')).eq(True)].ffill(1).iloc[:,-1]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用特定字符串搜索 dataframe 中的所有值 - How to search all the values in a dataframe with a particular string 在 pandas dataframe 列中搜索特定的字符串集,然后搜索该字符串 - Search pandas dataframe column for particular set of string, and then that string 从 dataframe 的列中提取字符串并使用该字符串添加新列 - Extract a string from a column of a dataframe and add a new column using that string 如何从 dataframe 中的字符串中提取数字并将这些数字的倍数添加到同一 dataframe 的新列中 - How does one extract numbers from a string in a dataframe and add the multiple of these numbers in a new column of the same dataframe 如何从 python 中的 dataframe 中搜索和提取特定值? - How can I search for and extract a particular value from a dataframe in python? Pandas 数据框解析字符串列以将日期提取到新列中 - Pandas dataframe parse string column to extract dates into new column Pandas 从 dataframe 中的列中提取部分字符串并将其存储在新列中 - Pandas extract part of string form a column in a dataframe and store it in a new column Pandas 添加一个带有字符串的新列,其中单元格匹配特定条件 - Pandas add a new column with a string where the cell match a particular condition 如何在熊猫数据框中搜索字符串并与另一个匹配? - How to search for a string in pandas dataframe and match with another? 如何从一列中提取字符串并将其保存在 pandas dataframe 的新列中? - How to extract a string from one column and save it in a new column in pandas dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM