熊猫打印到latex的数据框返回了意外的结果

2024-04-19 17:17:42 发布

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

我有一个熊猫数据框

>>> ris
                   Pr Err    Tr Err    Pr Err    Tr Err    Pr Err    Tr Err
Data     nodes                                                             
boston   10      0.456532  0.458170  0.868836  0.810764  0.826163  0.775337
         30      0.512490  0.460417  0.623531  0.511936  0.600965  0.511689
         50      0.547000  0.447586  0.529250  0.377296  0.528913  0.398829
         100     0.781039  0.585664  0.452310  0.295945  0.480067  0.297470
         500     1.635246  1.084544  0.490629  0.188229  0.488209  0.174449
         1000    2.347967  1.325350  0.482844  0.153391  0.497174  0.139749
         5000    6.112860  3.248268  0.469981  0.141781  0.465061  0.055253
crime    10      0.713751  0.534806  0.959765  0.909708  0.964622  0.932691
         30      0.897192  0.648438  0.957707  0.814946  0.924469  0.835406
         50      1.259947  0.835315  0.958826  0.763053  0.935411  0.752043
         100     1.788571  1.188414  0.893760  0.675943  0.878403  0.638281
         500     3.705343  1.925542  0.875850  0.442439  0.917819  0.210832
         1000    5.364042  2.730365  0.877851  0.401714  0.834504  0.095997
         5000   11.592207  5.696515  0.924210  0.128621  0.757942  0.008422
diabetes 10      0.724064  0.708919  0.896751  0.884411  0.876967  0.871300
         30      0.776270  0.697421  0.748575  0.718090  0.770397  0.722314
         50      0.831268  0.738589  0.744160  0.670036  0.732472  0.681045
         100     0.944318  0.746765  0.725481  0.644612  0.739595  0.646943
         500     2.049824  1.295420  0.744597  0.638818  0.848764  0.342133
         1000    3.023125  1.945415  0.726805  0.632582  0.841408  0.202868
         5000    8.248108  4.950347  0.729375  0.614312  0.849506  0.049566
j2m      10      0.262522  0.220741  0.704471  0.707829  0.975908  0.971793
         30      0.329172  0.249799  0.612609  0.595846  0.937233  0.913272
         50      0.540852  0.392339  0.569175  0.550119  0.905082  0.870621
         100     0.837123  0.648585  0.537721  0.499987  0.846324  0.802037
         500     1.939758  1.398511  0.472884  0.354905  0.683411  0.496362
         1000    2.981468  2.151604  0.431983  0.307520  0.663899  0.339811
         5000    6.577426  4.737849  0.408002  0.290080  0.452132  0.022331
wine     10      0.821250  0.806193  0.940909  0.934531  0.940558  0.933438
         30      0.838295  0.817293  0.887308  0.862306  0.889729  0.876763
         50      0.897821  0.847260  0.872193  0.837761  0.872479  0.835796
         100     1.009768  0.909182  0.857645  0.814801  0.862258  0.809452
         500     1.675544  1.310232  0.855094  0.783809  0.940632  0.618788
         1000    2.094566  1.548781  0.863529  0.765906  0.973830  0.432291
         5000    5.004573  3.293214  0.861923  0.698045  0.920401  0.115565
>>> 

我想把这张桌子用在一个地方。我想打印它完全一样,但在乳胶格式。你知道吗

我试着用

print(ris.to_latex(float_format= lambda x: str(np.round(x, 3))))

我得到的结果不是我原来的数据帧。你知道吗

>>> \begin{tabular}{llrrrrrr}
\toprule
       &      &  Pr Err &  Tr Err &  Pr Err &  Tr Err &  Pr Err &  Tr Err \\
\midrule
boston & 10   &         &         &         &         &         &         \\
crime & 30   &   0.457 &   0.458 &   0.869 &   0.811 &   0.826 &   0.775 \\
diabetes & 50   &   0.512 &    0.46 &   0.624 &   0.512 &   0.601 &   0.512 \\
j2m & 100  &   0.547 &   0.448 &   0.529 &   0.377 &   0.529 &   0.399 \\
wine & 500  &   0.781 &   0.586 &   0.452 &   0.296 &    0.48 &   0.297 \\
\bottomrule
\end{tabular}

>>> 

我该怎么解决?你知道吗

编辑:

用命令print(ris.reset_index().to_latex(index=False))我得到:

\begin{tabular}{lrrrrrrr}
\toprule
     Data &  nodes &     Pr Err &    Tr Err &    Pr Err &    Tr Err &    Pr Err &    Tr Err \\
\midrule
   boston &     10 &   0.456532 &  0.458170 &  0.868836 &  0.810764 &  0.826163 &  0.775337 \\
   boston &     30 &   0.512490 &  0.460417 &  0.623531 &  0.511936 &  0.600965 &  0.511689 \\
   boston &     50 &   0.547000 &  0.447586 &  0.529250 &  0.377296 &  0.528913 &  0.398829 \\
   boston &    100 &   0.781039 &  0.585664 &  0.452310 &  0.295945 &  0.480067 &  0.297470 \\
   boston &    500 &   1.635246 &  1.084544 &  0.490629 &  0.188229 &  0.488209 &  0.174449 \\
   boston &   1000 &   2.347967 &  1.325350 &  0.482844 &  0.153391 &  0.497174 &  0.139749 \\
   boston &   5000 &   6.112860 &  3.248268 &  0.469981 &  0.141781 &  0.465061 &  0.055253 \\
    crime &     10 &   0.713751 &  0.534806 &  0.959765 &  0.909708 &  0.964622 &  0.932691 \\
    crime &     30 &   0.897192 &  0.648438 &  0.957707 &  0.814946 &  0.924469 &  0.835406 \\
    crime &     50 &   1.259947 &  0.835315 &  0.958826 &  0.763053 &  0.935411 &  0.752043 \\
    crime &    100 &   1.788571 &  1.188414 &  0.893760 &  0.675943 &  0.878403 &  0.638281 \\
    crime &    500 &   3.705343 &  1.925542 &  0.875850 &  0.442439 &  0.917819 &  0.210832 \\
    crime &   1000 &   5.364042 &  2.730365 &  0.877851 &  0.401714 &  0.834504 &  0.095997 \\
    crime &   5000 &  11.592207 &  5.696515 &  0.924210 &  0.128621 &  0.757942 &  0.008422 \\
 diabetes &     10 &   0.724064 &  0.708919 &  0.896751 &  0.884411 &  0.876967 &  0.871300 \\
 diabetes &     30 &   0.776270 &  0.697421 &  0.748575 &  0.718090 &  0.770397 &  0.722314 \\
 diabetes &     50 &   0.831268 &  0.738589 &  0.744160 &  0.670036 &  0.732472 &  0.681045 \\
 diabetes &    100 &   0.944318 &  0.746765 &  0.725481 &  0.644612 &  0.739595 &  0.646943 \\
 diabetes &    500 &   2.049824 &  1.295420 &  0.744597 &  0.638818 &  0.848764 &  0.342133 \\
 diabetes &   1000 &   3.023125 &  1.945415 &  0.726805 &  0.632582 &  0.841408 &  0.202868 \\
 diabetes &   5000 &   8.248108 &  4.950347 &  0.729375 &  0.614312 &  0.849506 &  0.049566 \\
      j2m &     10 &   0.262522 &  0.220741 &  0.704471 &  0.707829 &  0.975908 &  0.971793 \\
      j2m &     30 &   0.329172 &  0.249799 &  0.612609 &  0.595846 &  0.937233 &  0.913272 \\
      j2m &     50 &   0.540852 &  0.392339 &  0.569175 &  0.550119 &  0.905082 &  0.870621 \\
      j2m &    100 &   0.837123 &  0.648585 &  0.537721 &  0.499987 &  0.846324 &  0.802037 \\
      j2m &    500 &   1.939758 &  1.398511 &  0.472884 &  0.354905 &  0.683411 &  0.496362 \\
      j2m &   1000 &   2.981468 &  2.151604 &  0.431983 &  0.307520 &  0.663899 &  0.339811 \\
      j2m &   5000 &   6.577426 &  4.737849 &  0.408002 &  0.290080 &  0.452132 &  0.022331 \\
     wine &     10 &   0.821250 &  0.806193 &  0.940909 &  0.934531 &  0.940558 &  0.933438 \\
     wine &     30 &   0.838295 &  0.817293 &  0.887308 &  0.862306 &  0.889729 &  0.876763 \\
     wine &     50 &   0.897821 &  0.847260 &  0.872193 &  0.837761 &  0.872479 &  0.835796 \\
     wine &    100 &   1.009768 &  0.909182 &  0.857645 &  0.814801 &  0.862258 &  0.809452 \\
     wine &    500 &   1.675544 &  1.310232 &  0.855094 &  0.783809 &  0.940632 &  0.618788 \\
     wine &   1000 &   2.094566 &  1.548781 &  0.863529 &  0.765906 &  0.973830 &  0.432291 \\
     wine &   5000 &   5.004573 &  3.293214 &  0.861923 &  0.698045 &  0.920401 &  0.115565 \\
\bottomrule
\end{tabular}

Tags: to数据dataprbostontrnodesdiabetes