23

I am new to using pandas and I just don't know what to do with this :

I am using python. I have (properly) installed anaconda. In my file I simply create a DataFrame (first by importing it from read_csv, then recreating it by hand to make sure that was not the problem). When I do print (dataframe) it prints:

        km |  price
0  | 240000 |  3650

[...]

23  | 61789 |  8290

When I do dataframe.info() I get this :

class 'pandas.core.frame.DataFrame'

Int64Index: 24 entries, 0 to 23

Data columns (total 2 columns):

km       24 non-null int64

price    24 non-null int64

dtypes: int64(2)

memory usage: 576.0 bytes

Which is perfect. But any other simple function I try just displays NOTHING. I tried dataframe.head(), dataframe['km'], dataframe[3:6], etc. No errors, just a big bowl of nothing on my terminal.

Edit to add example code:

import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt 
pd.set_option('max_columns', 50) 
#df=pd.read_csv('data.csv') 
data = {'km': [240000, 139800, 150500, 185530, 176000, 114800, 166800, 89000, 144500, 84000, 82029, 63060, 74000, 97500, 67000, 76025, 48235, 93000, 60949, 65674, 54000, 68500, 22899, 61789], 'price': [3650, 3800, 4400, 4450, 5250, 5350, 5800, 5990, 5999, 6200, 6390, 6390, 6600, 6800, 6800, 6900, 6900, 6990, 7490, 7555, 7990, 7990, 7990, 8290]} 
df = pd.DataFrame(data, columns=['km', 'price']) 
print (df) 
df.info() 
df[2:5] 
df["km"] 
iayork
  • 6,420
  • 8
  • 44
  • 49
Lauref
  • 331
  • 1
  • 2
  • 4
  • 1
    You'll need to show a small but complete, self-contained, reproducible example demonstrating the problem. – BrenBarn Nov 02 '15 at 17:47
  • 1
    What is the result of dataframe.to_dict()? – Alexander Nov 02 '15 at 17:54
  • Here's the complete code: import pandas as pd import numpy as np import matplotlib.pyplot as plt pd.set_option('max_columns', 50) #df=pd.read_csv('data.csv') data = {'km': [240000, 139800, 150500, 185530, 176000, 114800, 166800, 89000, 144500, 84000, 82029, 63060, 74000, 97500, 67000, 76025, 48235, 93000, 60949, 65674, 54000, 68500, 22899, 61789], 'price': [3650, 3800, 4400, 4450, 5250, 5350, 5800, 5990, 5999, 6200, 6390, 6390, 6600, 6800, 6800, 6900, 6900, 6990, 7490, 7555, 7990, 7990, 7990, 8290]} df = pd.DataFrame(data, columns=['km', 'price']) print (df) df.info() df[2:5] df["km"] – Lauref Nov 02 '15 at 18:02
  • @Alexander : that does work, it returns a dictionary that I can use. It doesn't explain why the pandas' functions won't work but at least I can use my data. Thanks ! – Lauref Nov 02 '15 at 18:13
  • actually you are missing some thing coz you have converted dictionary from pandas dataframe object! – WoodChopper Nov 02 '15 at 18:17

2 Answers2

59

You have to use:

print(dataframe.head())
print(dataframe['km'])
print(dataframe[3:6])

Without the print statement python is just selecting the data but not doing anything with it.

Ryan
  • 591
  • 4
  • 3
  • My first foray into python where the pandas (and mutagen) documentation shows examples in the interactive shell where you don't need to explicitly wrap "print()" around actions like dataframe.head(). But I was running my code as a shebang script. I was really confused. – Jim Dec 08 '22 at 18:07
6

You may expect this would show some row after seeing how other does. But this only appear only in terminal environment. Just like the above answer, you need an print() to show row while you are using ide

jason
  • 133
  • 2
  • 8