0

I am doing the Titanic problem in Kaggle and I have problems displaying the dataframe:

import pandas as pd
import numpy as np

titanic = pd.read_csv("input/train.csv")
titanic.head()

This should display the train.csv but it doesn't. Do you know why?

cchamberlain
  • 17,444
  • 7
  • 59
  • 72
Nacho
  • 35
  • 5

2 Answers2

1

Whether you are using the REPL in Sublime Text or just running the program, you can display a dataframe called titanic as:

# prints first 5 rows in dataframe format
print(titanic.head())

# prints all rows in dataframe format
print(titanic)

If you want to display the data frame in CSV format, you need to convert it to CSV first using the to_csv function:

# prints first 5 rows in CSV format
print(titanic.head().to_csv())

# prints all rows in CSV format
print(titanic.to_csv())
shish023
  • 533
  • 3
  • 10
0

Are you executing this from REPL prompt or as a script? If in REPL, it should print in prompt or if through script, try this. df.head().to_csv(sys.stdout).

The to_csv(..) method takes a filepath or buffer. With this, you are redirecting output to stdout. Please ensure to import sys module.

kuriouscoder
  • 5,394
  • 7
  • 26
  • 40