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