Pandas boolean comparisson on dataframe

前端 未结 3 1400
盖世英雄少女心
盖世英雄少女心 2020-12-21 19:44

I am getting the error when I make a comparison on a single element in a dataframe, but I don\'t understand why.

I have a dataframe df with timeseries data for a nu

3条回答
  •  爱一瞬间的悲伤
    2020-12-21 20:08

    Problem is you need compare scalar for return scalar (True, False), but there is one item Series, which is converted to one item boolean Series.

    Solutions is converting to scalar using Series.item or values with selecting first value by [0]:

    customer_ID = '8143511'
    ts = '2012-07-01 00:00:00'
    
    print (df[[customer_ID]].loc[ts].item())
    nan
    
    if pd.isnull(df[[customer_ID]].loc[ts]).item():
        print ('super')
    
    print (df[[customer_ID]].loc[ts].values[0])
    nan
    
    if pd.isnull(df[[customer_ID]].loc[ts]).values[0]:
        print ('super')
    

    But if use DataFrame.loc, get scalar (if not duplicated index or columns names):

    print (df.loc[ts, customer_ID])
    nan
    
    customer_ID = '8143511'
    ts = '2012-07-01 00:00:00'
    if pd.isnull(df.loc[ts, customer_ID]):
        print ('super')
    

提交回复
热议问题