【问题标题】:float() argument must be a string or a number, not 'Cell' - cannot solve issuefloat() 参数必须是字符串或数字,而不是 \'Cell\' - 无法解决问题
【发布时间】:2022-11-19 01:34:36
【问题描述】:

我在下面有以下代码。

我正在尝试存储R平方价值观和P值来自数据框中的 OLS 回归输出'排名变量列表'然后对这个数据帧进行排序,首先按 P 值,然后按 R 平方值。

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

我相信这可能是因为我的“R 平方”和“P 值”属于这种类型'细胞',我试图将它们转换为 float/int 但没有成功。

我将非常感谢您的帮助!

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
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                                         
==============================================================================

我将非常感谢您的帮助!

【问题讨论】:

    标签: python pandas jupyter-notebook


    【解决方案1】:

    以下代码有效 - 我没有将模型摘要输出转换为数据框,而是将模型摘要输出转换为 html 文件)。

    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
    

    【讨论】:

      猜你喜欢
      • 2020-04-24
      • 2022-06-14
      • 2020-10-04
      • 1970-01-01
      • 1970-01-01
      • 2023-02-18
      • 2019-05-22
      • 2023-04-09
      • 1970-01-01
      相关资源
      最近更新 更多