首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >参数必须是字符串或数字,而不是“单元格”-不能解决问题

参数必须是字符串或数字,而不是“单元格”-不能解决问题
EN

Stack Overflow用户
提问于 2022-11-18 14:40:36
回答 1查看 28关注 0票数 0

下面有下面的代码。

我试图将'rankedvariableslist'中的R-平方值和P-值从OLS回归输出中存储,然后根据P值,然后根据R-平方值对数据进行排序。

但是,我得到了错误:'float()参数必须是字符串或数字,而不是'Cell''

我相信这可能是因为我的“R-平方”和“P-值”是'cell'类型的,我试图将它们转换为浮动/int,但没有成功。

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

代码语言: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 = pd.DataFrame({'Sleepvariables':correspondantsleepvariable, 'R-squared value':rsquaredvalues,'P-value':correspondantpvalue})
listed = list(range(0, 21))
listed = pd.DataFrame(listed)
rankedvariableslist = pd.concat((rankedvariableslist,listed),axis=1)
rankedvariableslist = rankedvariableslist.rename(columns={0: "Value"})
rankedvariableslist['R-squared value'] = rankedvariableslist['R-squared value'].astype('category').cat.as_ordered()
rankedvariableslist['P-value'] = rankedvariableslist['P-value'].astype('category').cat.as_ordered()
rankedvariableslist['Sleepvariables'] = rankedvariableslist['Sleepvariables'].astype('category').cat.as_ordered()
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

回答 1

Stack Overflow用户

发布于 2022-11-18 17:34:36

下面的代码起作用了--我没有将模型摘要输出转换为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/74491328

复制
相关文章

相似问题

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