Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将循环打印输出更改为数据帧_Python_Pandas_Loops_Printing - Fatal编程技术网

Python 如何将循环打印输出更改为数据帧

Python 如何将循环打印输出更改为数据帧,python,pandas,loops,printing,Python,Pandas,Loops,Printing,我想将以下for循环的输出打印到一个漂亮的数据框中: for i in range (1911, 2020): bestmovie= movie.loc[movie['year'] == i] bestmovie = bestmovie[['year','title', 'avg_vote', 'genre']].sort_values(['avg_vote'], ascending = False) bestmovie = bestmovie.head(10)

我想将以下for循环的输出打印到一个漂亮的数据框中:

for i in range (1911, 2020):
    bestmovie= movie.loc[movie['year'] == i]
    bestmovie = bestmovie[['year','title', 'avg_vote', 'genre']].sort_values(['avg_vote'], ascending = False)
    bestmovie = bestmovie.head(10)
    print (bestmovie)
输出如下所示:

         year                 title  avg_vote                      genre
3      1911             L'Inferno       7.0  Adventure, Drama, Fantasy
38007  1911           Karadjordje       6.3                 Drama, War
34547  1911  Oborona Sevastopolya       6.0               History, War
1      1911        Den sorte drøm       5.9                      Drama
       year                                              title  avg_vote  \
40716  1912                     Le mystère des roches de Kador       7.0   
7      1912                              Independenta Romaniei       6.7   
4      1912  From the Manger to the Cross; or, Jesus of Naz...       5.7   
8      1912                                        Richard III       5.5   
2      1912                                          Cleopatra       5.2   

                       genre  
40716  Crime, Drama, Mystery  
7               History, War  
4           Biography, Drama  
8                      Drama  
2             Drama, History  

如何将其更改为熊猫数据帧格式?

我过去有过类似的输出,结果是返回一个元素列表。那个元素恰好是一个数据帧。要检查代码中是否发生了相同的情况,请运行以下命令

print(type(bestmovie))
print(type(bestmovie[0]))
 bestmovie = bestmovie[0]
如果以上结果

<class 'list'>
<class 'pandas.core.frame.DataFrame'> 

我认为您的显示器打印数据帧的宽度有问题,这就是它跳转的原因。我很确定数据是在一个完美的数据框架中。这能回答你的问题吗?嗨,克里斯,你说得对,这是一个数据帧。我的问题是它看起来的样子。使用“打印”功能会丢失结构化表格格式。如果我只是把“bestmovie”放在末尾,没有“print”和tab,那么输出就在一个很好的表格中,但是它只打印去年的内容。。。有没有办法以这种格式打印所有年份?打印(键入(bestmovie))会发生什么?