首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按多列排序-“值”没有排序,请通过传递类别参数显式指定类别顺序。

按多列排序-“值”没有排序,请通过传递类别参数显式指定类别顺序。
EN

Stack Overflow用户
提问于 2022-11-18 11:33:57
回答 2查看 31关注 0票数 0

我有数据'rankedvariableslist',,索引‘睡眠变量’是感兴趣的睡眠变量,两列分别是模型和变量的R-平方和P-值。

我试图按‘P-值’排序数据,然后按‘R-平方值’排序,但我一直得到错误:‘’值没有排序,请通过传递类别参数来显式指定类别顺序,但不确定原因。

如果能伸出援手,我将不胜感激!

代码语言:javascript
复制
correspondantsleepvariable = []
correspondantpvalue = []
correspondantpvalue = [] 

newerresults = resultmodeldistancevariation2sleepsummary.tables[0]
newerdata = pd.DataFrame(newerresults)
rsquaredvalue = newerdata.iloc[0,3]
rsquaredvalues.append(rsquaredvalue)
modelpvalues = resultmodeldistancevariation2sleepsummary.tables[1]
newerdatavalues = pd.DataFrame(modelpvalues)
pvalue = newerdatavalues.iloc[12,4]
correspondantpvalue.append(pvalue)
correspondantsleepvariable.append(sleepvariable[i])
rankedvariableslist.sort_values(['P-value','R-squared value'],ascending = [True, False])
print(rankedvariableslist.head(3)

                         Sleepvariables  R-squared value P-value
0                        hours_of_sleep           0.026   0.491
1              frequency_of_alarm_usage           0.026   0.681
2                        sleepiness_bed           0.026   0.413
代码语言:javascript
复制
As an example of the dataframe 'newerresults':

                            OLS Regression Results                            
==============================================================================
Dep. Variable:               distance   R-squared:                       0.028
Model:                            OLS   Adj. R-squared:                  0.016
Method:                 Least Squares   F-statistic:                     2.338
Date:                Fri, 18 Nov 2022   Prob (F-statistic):            0.00773
Time:                        12:39:29   Log-Likelihood:                -1274.1
No. Observations:                 907   AIC:                             2572.
Df Residuals:                     895   BIC:                             2630.
Df Model:                          11                                         
Covariance Type:            nonrobust                                         
==============================================================================
EN

回答 2

Stack Overflow用户

发布于 2022-11-18 12:01:58

您的代码在我运行时正常工作,它将返回以下结果。

代码语言:javascript
复制
                         Sleepvariables  R-squared value P-value
0                        hours_of_sleep           0.026   0.413
1              frequency_of_alarm_usage           0.026   0.491
2                        sleepiness_bed           0.026   0.681
票数 0
EN

Stack Overflow用户

发布于 2022-11-18 17:32:03

下面的代码起作用了--我没有将模型摘要输出转换为dataframe,而是将模型摘要输出转换为html文件)。

代码语言:javascript
复制
correspondantsleepvariable = []
correspondantpvalue = []
correspondantpvalue = [] 

results_as_html = resultmodeldistancevariation2sleepsummary.tables[0].as_html()
datehere = pd.read_html(results_as_html, header=None, index_col=None)[0]
rsquaredvalue = datehere.iloc[0,3]
rsquaredvalue.astype(float)
rsquaredvalues.append(rsquaredvalue)
results_as_html = resultmodeldistancevariation2sleepsummary.tables[1].as_html()
datehere = pd.read_html(results_as_html, header=0, index_col=0)[0]
pvalue = datehere.iloc[11,3]
pvalue.astype(float)
correspondantpvalue.append(pvalue)
correspondantsleepvariable.append(sleepvariable[i])
rankedvariableslist = 
pd.DataFrame({'Sleepvariables':correspondantsleepvariable, 'R-squared value':rsquaredvalues,'P-value':correspondantpvalue})
rankedvariableslist.sort_values(by=['P-value','R-squared value'],ascending = [True,False],inplace=True)
print(rankedvariableslist)

Sleepvariables  R-squared value  P-value
9    time_spent_awake_during_night_mins            0.034    0.005
4                         sleep_quality            0.030    0.041
20          sleepiness_resolution_index            0.028    0.129

非常感谢你的帮助--我很感激!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74489047

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档