【问题标题】:TypeError: sort_values() missing 1 required positional argument: 'self'TypeError: sort_values() 缺少 1 个必需的位置参数:\'self\'
【发布时间】:2022-12-11 01:05:31
【问题描述】:

上述问题的解决方案是什么?

Given a dataframe, you have to sort the rows based on values of one column.
Note: Sorting should be in descending order of values of given column
The input will contain a column name. The output should contain the first five rows of the dataframe.
The output will contain the first n rows of the sorted dataframe.
Sample Input:
TOEFL Score
Sample Output:

     Serial No.  GRE Score  TOEFL Score  University Rating  SOP  LOR   CGPA  \
25           26        340          120                  5  4.5   4.5  9.60   
97           98        331          120                  3  4.0   4.0  8.96   
81           82        340          120                  4  5.0   5.0  9.50   
202         203        340          120                  5  4.5   4.5  9.91   
203         204        334          120                  5  4.0   5.0  9.87   

     Research  Chance of Admit   
25          1              0.94  
97          1              0.86  
81          1              0.96  
202         1              0.97  
203         1              0.97  
Execution Time Limit

将熊猫导入为 pd 列=输入() df=pd.read_csv("https://media-doselect.s3.amazonaws.com/generic/RM8r5NBrJdA4QeVZXvwbjokwv/Admission_Predict.csv") #在这里写你的代码 排序 = pd.DataFrame.sort_values(by="col", axis=0, ascending=False) 打印(排序)

【问题讨论】:

    标签: python pandas dataframe numpy


    【解决方案1】:

    柱子。该方法采用以下参数:

    by:要排序的列的名称。 axis:要排序的轴(0 表示行,1 表示列)。 ascending:一个布尔值,指示是按升序还是降序排序。 下面是一个示例,说明如何使用 sort_values() 方法根据特定列的值对 DataFrame 进行排序:

    import pandas as pd
    

     Read the data from a CSV file
    df = pd.read_csv("https://media-doselect.s3.amazonaws.com/generic/RM8r5NBrJdA4QeVZXvwbjokwv/Admission_Predict.csv")
    
    # Sort the rows of the DataFrame based on the values of the "TOEFL Score" column
    sort = df.sort_values(by="TOEFL Score", axis=0, ascending=False)
    
    # Print the first five rows of the sorted DataFrame
    print(sort.head())
    

    在此示例中,sort_values() 方法根据“TOEFL Score”列的值按降序对 DataFrame 的行进行排序。然后它使用 head() 方法打印排序后的 DataFrame 的前五行。

    您可以修改此代码以解决问题,方法是将硬编码的列名称“TOEFL Score”替换为用户提供的列名称作为输入。然后,您可以使用 head() 方法打印排序后的 DataFrame 的前五行,如上例所示。

    【讨论】:

      猜你喜欢
      • 2013-07-06
      • 2020-02-15
      • 2021-12-15
      • 2023-02-06
      • 1970-01-01
      • 2021-09-15
      • 2022-01-05
      相关资源
      最近更新 更多