我想对df排序,但我想从排序中排除前两列。
我可以在排除前1列的同时成功排序,但我想更新下面的代码以排除前2列。
from pandas import DataFrame
Cars = {'Dimensions': [0.48,0.44,0.4,0.6],
'Price': [0.3,0.25,0.74,0.5],
'Year': [0.41,0.38,0.64,0.65],
'Range': [0.95,0.98,0.24,0.42],
'Height': [0.75,0.88,0.84,0.95],
}
df = DataFrame(Cars, columns= ['Dimensions', 'Price','Year', 'Range', 'Height'],
index=['Honda Civic','Toyota Corolla','Ford Focus','Audi A4'])
s = df.iloc[0]
df = df.iloc[:, ((-s[1:]).argsort() + 1).reindex(df.columns, fill_value=0)]
我原以为这能行,但没用
s = df.iloc[0:1]
df = df.iloc[:, ((-s[1:]).argsort() + 1).reindex(df.columns, fill_value=0)]
错误:
AttributeError: 'DataFrame' object has no attribute 'argsort'
预期产量:
Dimensions Price Range Height Year
Honda Civic 0.48 0.30 0.95 0.75 0.41
Toyota Corolla 0.44 0.25 0.98 0.88 0.38
Ford Focus 0.40 0.74 0.24 0.84 0.64
Audi A4 0.60 0.50 0.42 0.95 0.65