-1

in the following code only the plot is drawn but the head is not printed why?

import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("file.csv")
df.set_index("id", inplace=True)
plt.plot(df)
plt.show() # this draws plof of entire df form csv
print(df.head(10)) # this does not print the first 10 rows of the dataframe
mCs
  • 2,591
  • 6
  • 39
  • 66

2 Answers2

0

You need assign back if want plot only first 5 rows:

df = df.head()
plt.plot(df)

Or:

plt.plot(df.head())

Pandas DataFrame.plot:

df.head().plot()

But if want see first 5 rows use:

print(df. head())

And if want plot table with graph together use this solution with small change:

clust_data = df.head().values
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • @jazreal I have edited the question. So I want to print plot of the entire df and additionally print `head` of say 10 first rows. Now it only draws the plot. – mCs May 13 '18 at 12:09
  • PS When I delete the plt lines (plt.plot(df) plt.show() ) the `head` will be printed as expected though – mCs May 13 '18 at 12:11
  • ok when I swithc it the `print(df.head(10))` is printed and the plot is drawn – mCs May 13 '18 at 12:18
  • @mCs - I think another solution is exactly what need. – jezrael May 13 '18 at 12:19
  • Ok in Jupyter the `print(df.head(10))` and then plot works fine but in PyCharm it is not – mCs May 13 '18 at 12:21
  • @mCs - I dont use `pycharm`, but I find [this](https://stackoverflow.com/q/43966427/2901002), maybe help. – jezrael May 13 '18 at 12:24
0

In an evironment where plt.show() blocks the script (due to it starting an event loop), which would be the case when running this as script or from the console, you either need to

  • close the figure first
  • Print the values before showing the figure,

    plt.plot(df)
    print(df.head(10))
    plt.show()
    
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • The problem is with PyCharm `plt.plot(df) print(df.head(10)) plt.show() ` still only draws the plot – mCs May 13 '18 at 12:20
  • The fact that you're using PyCharm is important, you miss to state that in the question. I think PyCharm has a lot of problems, which is why I'm not using it at all. But of course there might be a solution to it, which I'm not aware of. – ImportanceOfBeingErnest May 13 '18 at 12:25
  • Thank you I have attached this info to the topic. Can you recommend me then another reliable and efficient tool? posibly the one you are using? – mCs May 13 '18 at 12:29
  • I am using Spyder rutinely. I cannot recommend anything, what works for one doesn't for someone else. – ImportanceOfBeingErnest May 13 '18 at 12:42