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

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

我不知道这里出了什么问题。

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]

我收到错误。我的真实数据框也会发生这种情况...

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

希望大家指教解决。谢谢!

如果可能,select 一行,例如其次是 1 转换为 numpy 数组,因为使用了 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

如果使用 select - 一行 DataFrame 而不是 Series 通过索引展平值:

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]