This question already has answers here:
Why does my Pandas DataFrame not display new order using `sort_values`?
                                
                                    (2个答案)
                                
                        
                                2年前关闭。
            
                    
我正在按学生证号对数据进行排序。当我明确地提出争论inplace = True时。我收到以下错误-

  ValueError: This Series is a view of some other array, to sort in-place you must create a copy


我想将排序后的数据保存到文件中,因此无法使inplace = False。
我不明白为什么它显示错误。
这是我的代码-

    df = pd.read_csv('/home/user/Documents/MOOC dataset test/students_info_assessment2.csv')
    df = df.id_student.sort_values(inplace=True)
    df = pd.DataFrame(df)
    df.to_csv('/home/user/Documents/MOOC dataset test/students_info_assessment_sorted.csv')


我该怎么办?

最佳答案

您可以使用此:

df = df.sort_values(by=['id_student'])

10-04 10:57
查看更多