How to print up to 40 rows in DataFrame

Question:

I’m trying to get data from csv and output it to the console (ie, command line).
I have 30 columns, but I can only output 5 to 6 columns.

df = pd.read_csv(csv_raw)

print(df.head())

         date        level   mark       source  
0  2022-01-01     A        1         facebook
1  2022-01-01     B        2         facebook
2  2022-01-01     C        12       facebook
3  2022-01-01     D        53       facebook
4  2022-01-01     T        22       facebook

If I display all 30 columns it turns out like this:

print(df.head(30))

          date       ...    source
0   2022-01-01  ...  facebook
1   2022-01-01  ...  facebook
2   2022-01-01  ...  facebook
3   2022-01-01  ...  facebook
4   2022-01-01  ...  facebook
5   2022-01-01  ...  facebook

when i try pd.options.display.max_columns = 50
it returns me like that:

         date                               level  clicks  
0  2022-01-01                                 A       1   
1  2022-01-01                                 B       2   
2  2022-01-01                                 C       12   
3  2022-01-01                                 D       53   
4  2022-01-01                                 T       22   
5  2022-01-01  Free trial, upgrade to basic at https://www.wi...       1   

     source  
0   facebook  
1   facebook  
2   facebook  
3   facebook  
4   facebook  
5   facebook  

Is it possible somehow to display more than 5 columns as in the first case?

Asked By: kegob53929

||

Answers:

There are 3 dataframe settings to be set to display the desired output

(1) Set the overall width (number of characters)
pd.options.display.width = 500
pd.options.display.width = None #for unlimited

(2) Set the maximum columns count (number of columns)
pd.options.display.max_columns = 50
pd.options.display.max_columns = None #for unlimited

(3) Set the maximum width of each column (number of characters)
pd.options.display.max_colwidth = 30
pd.options.display.max_colwidth = None #for unlimited

There is a row (index 5) having the value Free trial, upgrade to basic at https://www.wi... which is making a mess of the columns. To delete this row, use:
df.drop(5, inplace=True)

Answered By: perpetualstudent
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.