Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 验证数据帧列_Python_Pandas - Fatal编程技术网

Python 验证数据帧列

Python 验证数据帧列,python,pandas,Python,Pandas,我有一个数据框,列如下- u'wellthie_issuer_identifier', u'issuer_name', u'service_area_identifier', u'hios_plan_identifier', u'plan_year', u'type' 我需要验证每列中的值,最后得到一个有效的数据帧 例如,我需要检查plan\u year列是否满足下面的验证 presence: true, numericality: true, length: { is: 4 } hios\

我有一个数据框,列如下-

u'wellthie_issuer_identifier', u'issuer_name', u'service_area_identifier', u'hios_plan_identifier', u'plan_year', u'type'
我需要验证每列中的值,最后得到一个有效的数据帧

例如,我需要检查
plan\u year
列是否满足下面的验证

presence: true, numericality: true, length: { is: 4 }
hios\u计划\u标识符
列满足下面的正则表达式

          format: /\A(\d{5}[A-Z]{2}[a-zA-Z0-9]{3,7}-TMP|\d{5}[A-Z]{2}\d{3,7}(\-?\d{2})*)\z/,
          presence: true, length: { minimum: 10 },
类型
列包含

in: ['MetalPlan', 'MedicarePlan', 'BasicHealthPlan', 'DualPlan', 'MedicaidPlan', 'ChipPlan']
我需要验证很多列。我试图给出一个数据示例

我能够用s
tr.contains('\A(\d{5}[A-Z]{2}[A-zA-Z0-9]{3,7}-TMP{124;\ d{5}[A-Z]{2}\d{3,7}(\-?\d{2})*\Z},regex=True)检查正则表达式。


同样,我也可以单独检查其他验证。我不知道如何把所有的验证放在一起。我是否应该在
条件下,将所有内容放入
if
循环中。是否有一种简单的方法来验证dataframe列?此处需要帮助

您可以使用多个功能。基本上,您可以使用以下语法按内容过滤数据帧:

df = df[(condition1) & (condition2) & ...] # filter the df and assign to the same df
特别针对您的情况,您可以使用以下函数(表达式)替换
条件

最后,如果要重置索引,可以使用
df=df.reset\u index(drop=True)
将输出df索引重置为0,1,2

编辑:检查NaN、NaT、None值是否可以使用

df[some_column].isnull()
对于多个列,可以使用

df[[col1, col2]].isin(valuelist).all(axis=1)

如何检查特殊列是否为空?使用is not null?
df[some_column].isnull()
可以检查NaN、NaT和None,但不能检查inf和空字符串,对于空字符串,您可以检查长度而不是Kevin,
((df['plan_year'].notnull().any())和(df['plan_year'].str.isdigit())和(df['plan_year']str.len()=4))
。这是正确的方法吗?或者我可以把它分组吗。另外,这也让我觉得
只能使用带字符串值的.str访问器,
错误。@user1896796您不需要
.any()
,并且在使用
.str.isdigit()
之前,您必须确保您的列类型是字符串,如果您不确定是否可以执行
.astype(str).str.isdigit()
谢谢Kevin。请再帮我一个忙。我在多个列上有相同的验证集。我是否可以将所有列组合在一起,或者我必须一次又一次地验证所有列?
df[[col1, col2]].isin(valuelist).all(axis=1)