简体   繁体   English

根据列的值对列进行排序。 熊猫

[英]Sorting the columns based on value of a column. Pandas

df1 df1

ITEM      CATEGORY       COLOR     LOCATION      PRICE

23661      BIKE          BLUE        A           30000
23661      BIKE          BLUE        B           43563
23661      BIKE          BLUE        C           45124
23661      BIKE          BLUE        D           28000
48684      CAR           RED         B           45145
48684      CAR           RED         D           35613
48684      CAR           RED         A           82312
48684      CAR           RED         C           24536
48684      CAR           RED         E           45613
54519      BIKE          BLACK       A           21345
54519      BIKE          BLACK       B           62623
54519      BIKE          BLACK       C           14613
54519      BIKE          BLACK       E           14365
54519      BIKE          BLACK       D           67353

Expecting outcome is the location that has the highest price for the vehicle.预期结果是车辆价格最高的位置。

ITEM      CATEGORY       COLOR     LOCATION      PRICE

23661      BIKE          BLUE        C           45124
48684      CAR           RED         A           82312
54519      BIKE          BLACK       D           67353

df.sort_values(df['PRICE'], ascending=False, kind='quicksort') Using this code we can manually do one by one. df.sort_values(df['PRICE'], ascending=False, kind='quicksort')使用这段代码我们可以手动一一进行。 How to do this for the whole df.如何为整个 df 执行此操作。

sort_values + drop_duplicates : sort_values + drop_duplicates

df.sort_values(['ITEM','PRICE'],ascending=[True,False]).drop_duplicates('ITEM')

     ITEM CATEGORY  COLOR LOCATION  PRICE
2   23661     BIKE   BLUE        C  45124
6   48684      CAR    RED        A  82312
13  54519     BIKE  BLACK        D  67353

你可以对颜色列进行分组,然后对价格应用排序函数:类似这样的 df.groupby(["COLOR"]).apply(lambda x: x.sort_values(["PRICE"], Ascending = False))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 基于单列对熊猫中的多列进行排序 - sorting multiple columns in pandas based on a single column Pandas:根据不同列中的类别对多列的值进行分组。 并根据该类别的分组计算平均值 - Pandas: Group values of multiple columns according to categories in a different column. And calculate avg based on the grouping for that category 根据一列的排序对多个 Pandas Dataframe 列进行排序 - Sorting multiple Pandas Dataframe Columns based on the sorting of one column Pandas:根据列的值对行进行排序 - Pandas: Sorting rows based on a value of a column 如何根据列中的值递增值,并且每次递增时将值返回到另一列。 熊猫 - How do I increment value based on value in column and everytime I increment return the value to another column. Pandas 将多个列串联到新列中。 csv文件,python,熊猫 - Concatenate multiple columns into new column. csv file, python, pandas 基于现有列创建新列。 使用 1 或 0 - Create new columns based on an existing column. Use 1 or 0 根据另一列的行值创建列。 - Create column based on row value of another column. 根据值对列进行排序 - Sorting the Columns based on value Pandas - 展开 pandas 中的多个列并根据展开的列分配值 - Pandas - Explode multiple columns in pandas and assign value based on the exploded column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM