【发布时间】:2020-10-24 17:07:48
【问题描述】:
我想将以下 for 循环的输出打印到漂亮的 pandas 数据框:
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
如何将其更改为 pandas 数据框格式?
【问题讨论】:
-
我认为您的显示器可以打印数据框的宽度有问题,这就是它跳过的原因。我很确定数据在一个完美的数据框中。
-
嗨,克里斯,你是对的,它是一个数据框。我的问题是它的外观。使用“打印”功能,它会丢失结构化表格格式。如果我只是把“bestmovie”放在最后没有“打印”并且没有标签,那么输出会在一个很好的表格中,但是它只打印最后一年......有没有办法以这种格式打印所有年份?
标签: python pandas loops printing