21

I have the following code in PyCharm

import pandas as pd

import numpy as np

import matplotlib as plt

df = pd.read_csv("c:/temp/datafile.txt", sep='\t')

df.head(10)

I get the following output:

Process finished with exit code 0

I am supposed to get the first ten rows of my datafile, but these do not appear in PyCharm.

I checked the Project interpreter and all settings seem to be alright there. The right packages are installed (numpy, pandas, matplotlib) under the right Python version.

What am I doing wrong? Thanks.

furas
  • 134,197
  • 12
  • 106
  • 148

3 Answers3

43

PyCharm is not Python Shell which automatically prints all results.

In PyCharm you have to use print() to display anything.

print(df.head(10))

The same is when you run script in other IDE or editor or directly python script.py

furas
  • 134,197
  • 12
  • 106
  • 148
  • 1
    Thanks so much! :-) –  Oct 08 '16 at 19:17
  • @furas why is it that with some methods you don't need to use print then? For instance, df.info() displays the info just fine without needing any "print", whereas df.head() requires the print() wrapping it. – Bitcoin Cash - ADA enthusiast Apr 21 '17 at 07:45
  • 2
    @Tiago `df.info()` displays without `print()` because author of this method used `print()` inside this method. He decided that this method will be used only to display information. `df.head()` needs `print()` because it returns `DataFrame` which you can use in calculations before you display - i.e. `df2 = df.head() * 4` or `df2 = df.head()[-3]` – furas Apr 27 '17 at 05:37
1

For printing all data

print(df)

By Default it will print top 5 records for head.

print(df.head())

If you need 10 rows then you can write this way

print(df.head(10))

nazmul.3026
  • 918
  • 1
  • 9
  • 20
-1

I did File-Invalidate Caches/Restart Option Invalidate and after that I was able to get the head:

screenshot of python console

ascripter
  • 5,665
  • 12
  • 45
  • 68