0

I would like to print the output of the following for loop to a nice looking pandas dataframe:

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)

The output looks like this:

         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  

How can I change it to pandas dataframe format?

  • I think you have a problem with how wide your display can print the dataframe, which is why it's jumping over. I'm pretty sure the data is in a perfectly fine dataframe. – Chris Oct 24 '20 at 17:19
  • Does this answer your question? [How do I expand the output display to see more columns of a pandas DataFrame?](https://stackoverflow.com/questions/11707586/how-do-i-expand-the-output-display-to-see-more-columns-of-a-pandas-dataframe) – Chris Oct 24 '20 at 17:20
  • Hi Chris, you are right, it is a dataframe. My problem is the way it looks. with "print" function it loses the structured table format. If I just put "bestmovie" at the end without "print" and without tab the output is in a nice table, but then it prints only the last year... Is there any way to print all years in such a format? – Brigi Szabo Oct 25 '20 at 09:23

1 Answers1

0

I've had similar output in the past that turned out to be returning a list of one element. That one element happened to be a pandas dataframe. To check if the same is happening in your code, run the below.

print(type(bestmovie))
print(type(bestmovie[0]))

If the above yields...

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

...then, we had the same problem. To rectify, run the below.

 bestmovie = bestmovie[0]
Jim
  • 1
  • 2