尝试按降序排序值时出错

2024-10-02 08:29:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图按降序对值进行排序,但每次运行代码时都会抛出一个错误。 我试着对每个键值进行方差分析和F统计值排序,然后按降序对F统计值进行排序,直到排序部分工作正常为止

def PAregression() :
    m_p_values = result
    values=m_p_values.iloc[:,0].str.split('_', expand=True)
    m_p_values=pd.concat([values,m_p_values], axis=1)
    m_p_values.columns=['Parent','Sub','Texture','Orig','Values']
    m_p_values['Child']=m_p_values['Sub'].astype(str)+'_'+m_p_values['Texture'].astype(str)
    m_p_values=m_p_values[['Parent','Child','Values']]
    m_p_values.columns=['Parent','Child',Final1.iloc[0,-2]]

    Parent=[]
    Parent=pd.DataFrame(Parent)
    for group in m_p_values.groupby('Parent'): 
        Child=group[1]
        Child=pd.DataFrame(Child)
        Child=Child.fillna(0)
        Child=Child.sort_values(by=[Child.iloc[:,2],Child.iloc[:,1]], ascending=[False,True])
        Child=Child.iloc[0,:]
        Parent=pd.concat([Parent,Child],axis=1)

print(Parent)

Finals=[]
Finals2=[]
Finals=pd.DataFrame(Finals)
Finals2=pd.DataFrame(Finals)
for group in Final_check.groupby('Key'): 
    # group is a tuple where the first value is the Key and the second is the dataframe
    Final1=group[1]
    Final1=pd.DataFrame(Final1)
    result=regression()
    result2=PAregression()
    Finals=pd.concat([Finals, result], axis=1)
    Finals2=pd.concat([Finals2, result2], axis=1)

# do xyz with result

print(Finals2)

回溯

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-109-fa78de049ae7> in <module>
      8     Final1=pd.DataFrame(Final1)
      9     result=regression()
---> 10     result2=PAregression()
     11     Finals=pd.concat([Finals, result], axis=1)
     12     Finals2=pd.concat([Finals2, result2], axis=1)

<ipython-input-108-f3d7cc3edd81> in PAregression()
     14         Child=pd.DataFrame(Child)
     15         Child=Child.fillna(0)
---> 16         Child=Child.sort_values(by=[Child.iloc[:,2],Child.iloc[:,1]], ascending=[False,True])
     17         Child=Child.iloc[0,:]
     18         Parent=pd.concat([Parent,Child],axis=1)

~\anaconda3\lib\site-packages\pandas\core\frame.py in sort_values(self, by, axis, ascending, inplace, kind, na_position, ignore_index)
   4918             from pandas.core.sorting import lexsort_indexer
   4919 
-> 4920             keys = [self._get_label_or_level_values(x, axis=axis) for x in by]
   4921             indexer = lexsort_indexer(keys, orders=ascending, na_position=na_position)
   4922             indexer = ensure_platform_int(indexer)

~\anaconda3\lib\site-packages\pandas\core\frame.py in <listcomp>(.0)
   4918             from pandas.core.sorting import lexsort_indexer
   4919 
-> 4920             keys = [self._get_label_or_level_values(x, axis=axis) for x in by]
   4921             indexer = lexsort_indexer(keys, orders=ascending, na_position=na_position)
   4922             indexer = ensure_platform_int(indexer)

~\anaconda3\lib\site-packages\pandas\core\generic.py in _get_label_or_level_values(self, key, axis)
   1690             values = self.axes[axis].get_level_values(key)._values
   1691         else:
-> 1692             raise KeyError(key)
   1693 
   1694         # Check for duplicates

KeyError: 0    0.00
1    0.00
2    0.00
3    1.04
4    0.55
5    0.00
6    0.00
7    0.00
8    0.00
9    0.00
10   1.00
11   1.00
12   0.00
13   0.00
14   0.00
15   0.00
16   0.00
17   0.00
18   0.00
19   0.00
20   0.00
21   2.89
22   0.00
23   0.00
24   0.00
25   0.00
26   0.00
27   0.00
28   0.00
29   1.83
Name: 11003_LP SHIRT GODS & KINGS, dtype: float64

我不明白为什么会这样


Tags: inchilddataframeforresultparentpdvalues
1条回答
网友
1楼 · 发布于 2024-10-02 08:29:52

将整个序列作为by参数的值传递。相反,您需要传递列名称,而不是列本身:

Child = Child.sort_values(by=[Child.columns[2],Child.columns[1]], ascending=[False,True])

备选方案:by=Child.columns[2:0:-1].tolist()

相关问题 更多 >

    热门问题