【问题标题】:'DataFrame' object has no attribute 'sort''DataFrame' 对象没有属性 'sort'
【发布时间】:2017-10-22 18:16:05
【问题描述】:

我在这里遇到了一些问题,在我的 python 包中我安装了numpy,但我仍然有这个错误:

'DataFrame' 对象没有属性 'sort'

任何人都可以给我一些想法..

这是我的代码:

final.loc[-1] =['', 'P','Actual']
final.index = final.index + 1  # shifting index
final = final.sort()
final.columns=[final.columns,final.iloc[0]]
final = final.iloc[1:].reset_index(drop=True)
final.columns.names = (None, None)

【问题讨论】:

    标签: python pandas numpy dataframe


    【解决方案1】:

    sort() 在 DataFrames 中被弃用,取而代之的是:

    sort() 在版本 0.17 (2015-10-09) 的 Pandas 中已弃用(但仍然可用),引入了 sort_values()sort_index()。它已从版本 0.20 (2017-05-05) 的 Pandas 中删除。

    【讨论】:

    • 旧排序的默认行为是什么?这将有助于了解应该使用哪个新版本。旧文档对此似乎有点绝望,例如他们甚至没有说明使用默认参数 columns=None 时会发生什么。
    【解决方案2】:

    熊猫排序 101

    sort 在 v0.20 中已被 DataFrame.sort_valuesDataFrame.sort_index 替换。除此之外,我们还有argsort

    以下是排序中的一些常见用例,以及如何使用当前 API 中的排序函数来解决它们。首先,设置。

    # Setup
    np.random.seed(0)
    df = pd.DataFrame({'A': list('accab'), 'B': np.random.choice(10, 5)})    
    df                                                                                                                                        
       A  B
    0  a  7
    1  c  9
    2  c  3
    3  a  5
    4  b  2
    

    按单列排序

    例如,要按“A”列对 df 进行排序,请将 sort_values 与单个列名一起使用:

    df.sort_values(by='A')
    
       A  B
    0  a  7
    3  a  5
    4  b  2
    1  c  9
    2  c  3
    

    如果您需要新的 RangeIndex,请使用 DataFrame.reset_index

    按多列排序

    例如,要按df 中的“A”和“B”列排序,您可以将列表传递给sort_values

    df.sort_values(by=['A', 'B'])
    
       A  B
    3  a  5
    0  a  7
    4  b  2
    2  c  3
    1  c  9
    

    按数据帧索引排序

    df2 = df.sample(frac=1)
    df2
    
       A  B
    1  c  9
    0  a  7
    2  c  3
    3  a  5
    4  b  2
    

    您可以使用sort_index

    df2.sort_index()
    
       A  B
    0  a  7
    1  c  9
    2  c  3
    3  a  5
    4  b  2
    
    df.equals(df2)                                                                                                                            
    # False
    df.equals(df2.sort_index())                                                                                                               
    # True
    

    以下是一些与其性能相当的方法:

    %timeit df2.sort_index()                                                                                                                  
    %timeit df2.iloc[df2.index.argsort()]                                                                                                     
    %timeit df2.reindex(np.sort(df2.index))                                                                                                   
    
    605 µs ± 13.6 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
    610 µs ± 24.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
    581 µs ± 7.63 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
    

    按指数列表排序

    例如,

    idx = df2.index.argsort()
    idx
    # array([0, 7, 2, 3, 9, 4, 5, 6, 8, 1])
    

    这个“排序”问题实际上是一个简单的索引问题。只需将整数标签传递给 iloc 即可。

    df.iloc[idx]
    
       A  B
    1  c  9
    0  a  7
    2  c  3
    3  a  5
    4  b  2
    

    【讨论】:

      猜你喜欢
      • 2015-04-25
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      • 2021-07-14
      • 2013-10-23
      • 2021-01-08
      • 2021-11-01
      相关资源
      最近更新 更多