gpt4 book ai didi

python - 过滤混合类型和对象的列

转载 作者:行者123 更新时间:2023-12-01 07:56:35 24 4
gpt4 key购买 nike

我有一个数据框:

NPI.         hcps_code 
1003000126 92300
1003000126 G0101
1003000126. H0002

它还有 27 个其他功能和大约 900 万行,在“hcps_code”列中存在混合数据类型,其中一些是整数,一些是字符串,或者它们是组合的。

我需要根据匹配的数据框过滤数据框:

A) 适合范围 92920 到 93799

B) 匹配代码“H0002”或“G0101”

到目前为止,我尝试过这样做:

Surg_mammo_DA = super_clean_df.query('hcpcs_code == G0101')

但出现错误:

UndefinedVariableError: name 'G0101' is not defined

接下来我尝试:

Surg_mammo_DA = super_clean_df.filter(like='H0002', axis=0)

这会返回一个空数据框,因为该列的数据类型是对象,它无法识别它。

最后我尝试:

Surg_mammo_DA = super_clean_df.loc[(super_clean_df['hcpcs_code'] == 'H0002') &
(super_clean_df['hcpcs_code'] == 'G0101')]

由于对象冲突,这还会返回一个空数据帧。

所以我尝试更改列的数据类型:

super_clean_df.hcpcs_code = super_clean_df.hcpcs_code.astype(str)

但是它保持不变,很可能是因为该特征中存在 int 和 string/int 组合的值:

hcpcs_code                           object

有人知道如何根据一列的多个条件过滤数据集,并处理列中的不同数据类型吗?

最佳答案

编写两个处理数据类型和过滤器的条件

cond1 = pd.to_numeric(df['hcps_code'], errors = 'coerce').between(92920, 93799)
cond2 = df['hcps_code'].isin(['H0002', 'G0101'])
df[(cond1) | (cond2)]



NPI. hcps_code

1003000126 G0101
1003000126. H0002

关于python - 过滤混合类型和对象的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55940789/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com