Search code examples
python-3.xpandasvalueerror

during filtering of a pandas dataframe: ValueError: Buffer has wrong number of dimensions (expected 1, got 2)


I can't get a hold of what is going wrong here.

d = {'x' : [1,4,6,9],
     'y' : [1,4,6,8]}
df = pd.DataFrame(d)

#filter columns based on value in specific row
df_VIP = df.iloc[:,df.iloc[1:2,:]<3]

I get the error. An this also happens with my real dataframe...

ValueError: Buffer has wrong number of dimensions (expected 1, got 2)

I hope anyone has a hint to solve it. Thanks!


Solution

  • If possible, select by one row, e.g. second by 1 with convert to numpy array, because used iloc:

    d = {'x' : [1,4,6,9],
         'y' : [1,2,6,8]}
    df = pd.DataFrame(d)
    
    df_VIP = df.iloc[:,df.iloc[1,:].to_numpy()<3]
    print (df_VIP)
       y
    0  1
    1  2
    2  6
    3  8
    

    If use your select - one row DataFrame instead Series flatten values by indexing:

    print (df.iloc[1:2,:])
       x  y
    1  4  2
    
    
    print (df.iloc[1:2,:].to_numpy())
    [[4 2]]
    
    print (df.iloc[1:2,:].to_numpy()[0])
    [4 2]
    
    df_VIP = df.iloc[:,df.iloc[1:2,:].to_numpy()[0]<3]