在python中使用if条件时出现此错误。使用a.empty,a.bool(),a.item(),a.any()或a.all()

时间:2018-03-26 04:51:42

标签: python if-statement

如何解决这个问题,我在使用if条件调用函数时得到这个。我不知道我哪里出错了。任何人都可以指导我解决这个问题。

    path = pd.read_csv("/home/volumata/Documents/Sangeetha/Analytics/saved-test-data/telecom_whole.csv")
    #print(orginal_telecom_whole)
orginal_telecom_80p_train, orginal_telecom_80p_test = train_test_split(path, test_size=0.20)

data =  orginal_telecom_80p_train

data = orginal_telecom_80p_test

def read_data(path, data):

    if data == 'orginal_telecom_80p_train':
        return(head_tail(data))
    elif data == 'orginal_telecom_80p_test':
        return(data_info_for_analysis(data))

    return data

    #name = input("path","data2","data1")

def head_tail(data):

    "Head of the data"

    print(orginal_telecom_80p_train.head(5))

    "Tail of the data"

    print(orginal_telecom_80p_train.tail(5))

def data_info_for_analysis(data):

    "Shape of the data"

    print(orginal_telecom_80p_test.shape)

    "data describe"

    print(orginal_telecom_80p_test.describe())

    "max & min value"

    print(orginal_telecom_80p_test.max())

    print(orginal_telecom_80p_test.min())

    "Checking the datatypes"

    print(orginal_telecom_80p_test.dtypes)


if __name__ == '__main__':
    read_data(path, data)

将此错误发送到

ValueError:DataFrame的真值是不明确的。使用a.empty,a.bool(),a.item(),a.any()或a.all()。

如果是data =='orginal_telecom_80p_train',则需要首先显示条件部分,如果是data =='orginal_telecom_80p_test',则需要显示第二个if条件。

我的样本数据集

customerID  Date    gender  age region  SeniorCitizen   Partner Dependents  tenure  PhoneService    MultipleLines   InternetService OnlineSecurity  OnlineBackup    DeviceProtection    TechSupport StreamingTV StreamingMovies Contract    PaperlessBilling    PaymentMethod   MonthlyCharges  TotalCharges    Churn
9796-BPKIW  1/2/2008    Male    57  Northeast   1   No  No  8   Yes No  Fiber optic No  No  No  No  No  No  Month-to-month  Yes Credit card (automatic) 69.95   562.7   No
4298-OYIFC  1/4/2008    Male    50  South   0   Yes No  15  Yes Yes Fiber optic No  No  Yes Yes Yes Yes Month-to-month  Yes Electronic check    103.45  1539.8  No
9606-PBKBQ  1/6/2008    Male    85  Midwest 1   Yes No  32  Yes Yes Fiber optic No  No  No  No  No  Yes Month-to-month  Yes Electronic check    85  2642.05 Yes
1704-NRWYE  1/9/2008    Female  55  Midwest 1   No  No  9   Yes No  Fiber optic No  No  No  No  Yes No  Month-to-month  Yes Electronic check    80.85   751.65  Yes
9758-MFWGD  1/6/2008    Female  52  Northeast   1   Yes Yes 48  No  No  DSL No  Yes No  No  No  No  One year    No  Bank transfer (automatic)   29.9    1388.75 No
3777-XROBG  11/20/2012  Female  57  Northeast   0   Yes Yes 58  Yes No  No  No  No  No  No  No  No  Two year    No  Credit card (automatic) 19.55   1079.65 No
5863-OOKCL  1/6/2008    Female  50  Midwest 0   No  No  10  Yes No  No  No  No  No  No  No  No  Month-to-month  No  Mailed check    18.85   163.2   No
1766-GKNMI  1/6/2008    Male    53  Northeast   0   No  No  29  Yes Yes Fiber optic No  No  No  No  No  Yes Month-to-month  Yes Electronic check    84.9    2516.2  No
2592-YKDIF  1/6/2008    Female  78  Midwest 0   No  No  1   Yes No  No  No  No  No  No  No  No  Month-to-month  No  Mailed check    20.35   20.35   No

1 个答案:

答案 0 :(得分:0)

您正在比较data1和data2的值,因此该条件仅检查data1和data2是否不为0,您需要重新设计if条件,并且应该基于您决定data1 / 2是否有测试的方式或训练数据。