检查Pandas列值中是否有点,并更新状态列。

huangapple go评论53阅读模式
英文:

check if there is dot in the pandas column values and update the status column

问题

需要更新“status”列,对于其中“extr”列中不包含句点(.)的数据,在pandas中将其更新为'excep'。

代码:
import pandas as pd
import numpy as np
data_df = pd.DataFrame({'typ map': ['D', 'T','A', 'D'],
                         'extr': ['rpt ', 'abc.txt','[test].[att]', 'depot and '],
                         'status': ['vld', '','vld', 'depot and ']
                         })
data_df['status'] = np.where(((data_df['typ map'] =='D') & (data_df[~data_df['extr'].str.contains('.', na=False)])),'excep',data_df.status)

由于extr列中对于typ map列没有句点.),因此需要将其更新为'excep'
预期输出如下

   	typ map	        extr	       status
0	D	            rpt 	       excep
1	T	            abc.txt	
2	A	            [test].[att]	vld
3	D	            depot and 	   excep
英文:

Need to update the status column to 'excep' for which the data in the column 'extr' does not have a dot(.) in pandas.

Code:
import pandas as pd
import numpy as np
data_df = pd.DataFrame({'typ map': ['D', 'T','A', 'D'],
                         'extr': ['rpt ', 'abc.txt','[test].[att]', 'depot and '],
                         'status': ['vld', '','vld', 'depot and ']
                         })
data_df['status'] = np.where(((data_df['typ map'] =='D') & (data_df[~data_df['extr'].str.contains('.',na=False)])),'excep',data_df.status)

As there is no dot(.) in the 'extr' column for w.r.t column 'typ map', this has to be updated with 'excep'. 
Expected output is as below:
 
   	typ map	        extr	       status
0	D	            rpt 	       excep
1	T	            abc.txt	
2	A	            [test].[att]	vld
3	D	            depot and 	   excep


</details>


# 答案1
**得分**: 2

在代码中有一些错误:

  • 你应该在 str.contains 中使用 regex=False 参数(. 默认匹配任何字符)。
  • 你不应该用 str.contains 的输出对 data_df 进行切片,它返回一个布尔Series。
  • 不需要使用 np.where,只需在 loc 中对切片进行赋值。

data_df.loc[data_df['typ map'].eq('D')
&~data_df['extr'].str.contains('.', regex=False, na=False),
'status'] = 'excep'

输出:

typ map extr status
0 D rpt excep
1 T abc.txt
2 A [test].[att] vld
3 D depot and excep


<details>
<summary>英文:</summary>

You have a few errors in the code:
 - you should use the `regex=False` parameter of [`str.contains`](https://pandas.pydata.org/docs/reference/api/pandas.Series.str.contains.html) (`.` matches any character by default).
 - you shouldn&#39;t slice `data_df` with the output of `str.contains`, it returns a boolean Series.
 - `np.where` is not needed, just assign on a slice with [`loc`](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.loc.html).

data_df.loc[data_df['typ map'].eq('D')
&~data_df['extr'].str.contains('.', regex=False, na=False),
'status'] = 'excep'

Output:

typ map extr status
0 D rpt excep
1 T abc.txt
2 A [test].[att] vld
3 D depot and excep


</details>



huangapple
  • 本文由 发表于 2023年6月8日 01:09:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76425640.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定