验证数据框列数据

时间:2018-11-30 05:24:44

标签: python pandas dataframe

我可以使用

进行的常规验证
 m1 = (df[some_column] == some_value )
 m2 = ( df[some_column].isin(some_list_of_values) )# This check whether the value of the column is one of the values in the list
 m3 = ( df[some_column].str.contains() # You can use it the same as str.contains())
 m4 = (df[some_column].str.isdigit()) # Same usage as str.isdigit(), check whether string is all digits, need to make sure column type is string in advance

然后在所有上述验证之后获取数据框-

df = df[m1 & m2 & m3 & m4]

打印(df[some_column] == some_value )时得到

0 False
1 True
2 True

我想使用if来验证函数中的某些内容,例如

if min_group_price is True , then both single_male single_female needs to be True
If min_group_price is False , then no check(Final result should be True)

我的测试数据类似,

min_group_price single_male single_female 
0 1.0 2.0 3.0 
1 NaN NaN NaN 
2 1.0 2.0 NaN 
3 NaN 2.0 NaN 
4 0.0 NaN 4.0 
5 NaN NaN 2.0

按照上述逻辑,index 0,1,3,5应该为True。 我不想麻烦。我该怎么办?

1 个答案:

答案 0 :(得分:1)

您刚刚描述了一些布尔逻辑,可以通过熊猫轻松实现:

(~df['min_group_price'].notna()) | (
    df['single_male'].notna() & df['single_female'].notna())

0     True
1     True
2    False
3     True
4    False
5     True
dtype: bool

如果'min_group_price'不为null,则结果取决于'single_male'和'single_female'不为null,否则结果为True