-1

I have the following code snippet and output:

import pandas as pd
df = pd.DataFrame.from_dict({'col_1': list(range(100)), 'col_2': [list(range(20))]*100})
df.head()

table with 4 rows and col_2 being cut off at 13

is there a way I can customize the output of df.head() to have more rows, change the truncation of columns, etc.?

Warlax56
  • 1,170
  • 5
  • 30
  • 1
    One should always read [the docs](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.head.html) before asking – RichieV Sep 08 '20 at 16:55
  • 1
    I would make the argument that If you sat down and read the docs you would know everything, it would just take forever. I was surprised this question didn't exist on stack exchange, so I decided to ask it. I actually learned a thing or two as well – Warlax56 Sep 08 '20 at 17:09
  • 1
    I'm not advocating to read the full thing in one sit (which some people do, and learn a lot). But your specific question might not have been asked before because it is evident in the documentation... in fact it is _the only_ parameter that this function takes. I am a big fanatic of SO, here I learned the little coding I know, but questions that can be answered from official sources are not relevant here. --In other words, you want others to read the docs for you--- – RichieV Sep 08 '20 at 17:36
  • that's completely fair, and I see where you're coming from. I think what makes this question valid is that I didn't want to just change the number of rows, but also `change the truncation of columns, etc.`, which actually requires different approaches. The question was more so "how do a customize the total output of head()", not just "how do I change the number of rows on head". Therefore, I personally think this question has merit. – Warlax56 Sep 08 '20 at 19:20

2 Answers2

3

You can:

df.head(xxx)

xxx = number of rows

You can change other parameters as well with:

pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)
gtomer
  • 5,643
  • 1
  • 10
  • 21
0

You can customize virtually everything with pd.options.display. You set the general format with:

import pandas as pd
pd.options.display.max_columns = 50  # None -> No Restrictions
pd.options.display.max_rows = 10
pd.options.display.max_colwidth = 100
pd.options.display.precision = 3

df = pd.DataFrame.from_dict({'col_1': list(range(100)), 'col_2': [list(range(20))]*100, 'col_3': [0.12345]*100})

then the number of rows with:

df.head(40)

sample output

Warlax56
  • 1,170
  • 5
  • 30