1

I am testing pandas. I am downloading a csv from the internet and need to print out the entire csv. When I attempt to print, I get the first 5 lines and the last 5 lines, but not anything else in the middle. How can I print it all out?

#!/usr/local/bin/python3

import pandas as pd

california_housing_dataframe = pd.read_csv("https://download.mlcc.google.com/mledu-datasets/california_housing_train.csv", sep=",")
#california_housing_dataframe.describe()

print(california_housing_dataframe)
user2236794
  • 441
  • 3
  • 7
  • 22
  • 2
    Does this answer your question? [Pretty-print an entire Pandas Series / DataFrame](https://stackoverflow.com/questions/19124601/pretty-print-an-entire-pandas-series-dataframe) – Corentin Limier Feb 05 '20 at 16:14

2 Answers2

0

you would have to loop through it, The way it is right now is that it is only the headers. You have to loop through that and then loop through each item to get all the items of every column.

import pandas as pd

california_housing_dataframe = pd.read_csv("https://download.mlcc.google.com/mledu-datasets/california_housing_train.csv", sep=",")
#california_housing_dataframe.describe()

for i in california_housing_dataframe :
    for n in california_housing_dataframe[i]:
        print(n)

it should now print every item, tell me if there are any issues. Thanks.

pm980
  • 123
  • 11
  • The code you've posted will print each individual character in each column name and none of the dataframe data – G. Anderson Feb 05 '20 at 16:32
  • Doesn't it print out every item from every column? I believe by the data he meant everything inside the file? So this should be fine. – pm980 Feb 05 '20 at 16:37
  • Oh nevermind I see the mistake, Ill fix it up real quick. – pm980 Feb 05 '20 at 16:38
  • Have you run the code `for i in california_housing_dataframe: print(i)`? It's a common mistake to make, but that's not how you iterate dataframes. – G. Anderson Feb 05 '20 at 16:38
  • Yeah lol ill take an extra second to fix it, guess i wasnt thinking straight as I made it. – pm980 Feb 05 '20 at 16:41
  • Even your edit is...workable, but not best practice. Pandas has its own iteration method, `iterrows()`. For example, in the new code, you're printing down each column, rather than across rows. See the duplicate mentioned in the comments on the question itself for a better option if you're only printing. – G. Anderson Feb 05 '20 at 16:43
  • Oh I didnt know this lol, sorry man only been working with dataframes for a week so I didnt know about that function. my bad man – pm980 Feb 05 '20 at 16:46
  • Tbh though depending on what he is doing this should be fine, because at my job right now I've been using this simple bit of code to go through excels and its been working quite well I think – pm980 Feb 05 '20 at 16:47
  • No need to apologize for learning something new! But just food for thought: "its been working quite well" doesn't make something best practice. If the question was how to print each item in each column one-by-one, then this would be a perfect answer. But for printing an entire DF, then the answers in [Pretty-print an entire Pandas Series / DataFrame] are best practice – G. Anderson Feb 05 '20 at 16:51
  • True but in general I wouldnt be using this to just print out every item, I'd probably be placing items from columns into lists. this was just to show him that this is a way to get all the data, he chooses what to do with it – pm980 Feb 05 '20 at 16:53
0

You could try the to_string function

import pandas as pd

california_housing_dataframe = pd.read_csv("https://download.mlcc.google.com/mledu-datasets/california_housing_train.csv", sep=",")
#california_housing_dataframe.describe()
print(california_housing_dataframe.to_string())

It ain't perfect, but it does the job

GoatMode
  • 23
  • 2