简体   繁体   English

Pandas有条件的新列基于在其他数据框列中找到的期间

[英]Pandas Conditional new column based on period found in other dataframe column

I have a dataframe with file extensions. 我有一个带有文件扩展名的数据框。 Some have periods in them I am trying to create a new column flagging which ones contain a period or not conditionally. 有些人在其中有句点,我试图创建一个新的列,标记其中是否包含句点或无条件。 If I wanted to just get the rows that contain a period I would just use: send_rec_file_url[send_rec_file_url['file_name'].str.contains('\\.')] . 如果我只想获取包含句点的行,则可以使用: send_rec_file_url[send_rec_file_url['file_name'].str.contains('\\.')]

How do I create a new column like below? 如何创建如下所示的新列?

df
    file_name
0   png 
1   jpg
2   jpg
3   pdf
4   pdf
5   xlsx
6   docx.pdf
7   txt.scf
8   pdf
9   TXT.vbs
10  read_this.pdf 

Desired output: 所需的输出:

df
    file_name      has_period
0   png            no
1   jpg            no
2   jpg            no
3   pdf            no
4   pdf            no
5   xlsx           no
6   docx.pdf       yes
7   txt.scf        yes
8   pdf            no
9   TXT.vbs        yes
10  read_this.pdf  yes

You need to use the mask to change the value of the column. 您需要使用掩码来更改列的值。

df['has_period'] = 'no'
df.loc[df['file_name'].str.contains('\.'), 'has_period'] = 'yes'

Output: 输出:

           file_name has_period
0             png         no
1             jpg         no
2             jpg         no
3             pdf         no
4             pdf         no
5            xlsx         no
6        docx.pdf        yes
7         txt.scf        yes
8             pdf         no
9         TXT.vbs        yes
10  read_this.pdf        yes

You can try: 你可以试试:

df['has_period'] = ["Yes" if '.' in i else "No" for i in df['file_name']]

Output: 输出:

        file_name has_period
0             png         No
1             jpg         No
2             jpg         No
3             pdf         No
4             pdf         No
5            xlsx         No
6        docx.pdf        Yes
7         txt.scf        Yes
8             pdf         No
9         TXT.vbs        Yes
10  read_this.pdf        Yes

Note: pandas .str accessor is pretty slow, this solution should outperform .str accessor solutions. 注意:pandas .str访问器非常慢,此解决方案应优于.str访问器解决方案。

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

相关问题 基于在DataFrame中找到的字符串的Pandas New Column - Pandas New Column based on found string in a DataFrame 根据现有列的条件语句向熊猫数据框添加新列 - Add new column to a pandas dataframe based on conditional statement of existing column 基于数据框的其他列创建一个新的熊猫数据框列 - Create a new pandas dataframe column based on other column of the dataframe Pandas:根据 DataFrame 中的其他列在 DataFrame 中创建新列 - Pandas: Create new column in DataFrame based on other column in DataFrame 基于条件选择的新列,来自Pandas DataFrame中其他2列的值 - New column based on conditional selection from the values of 2 other columns in a Pandas DataFrame 根据 Pandas DataFrame 中其他列的条件创建新列 - Creating New Column based on condition on Other Column in Pandas DataFrame 如何根据 pandas dataframe 中其他列中的子字符串创建新列? - How to create new column based on substrings in other column in a pandas dataframe? pandas DataFrame中的新列取决于其他列的值 - New column in pandas DataFrame conditional on value of other columns 基于多个条件语句创建新列 pandas dataframe - creating new column based on multiple conditional statements pandas dataframe 基于条件语句在pandas数据框中生成新列 - Generate new column in pandas dataframe based on conditional statement
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM