英文:
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'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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论