0

I want to print my dataframe. Unfortunately the picture shows only 2 lines of the dataframe instead of the 20 lines and the table is below and there is a huge empty area as well.. Could someone help me to get all the 20 lines of the dataframe?

This is the source How to save a pandas DataFrame table as a png

import pandas as pd
import matplotlib.pyplot as plt
from pandas.plotting import table  # EDIT: see deprecation warnings below
from pathlib import Path


PATH_DATA = "../data"
PATH_RETAILROCKET = Path(PATH_DATA,"retailrocket/retailrocket/events.csv")
# RetailRocket
print(PATH_RETAILROCKET)
df = pd.read_csv(Path(PATH_RETAILROCKET))
df = df.head(20)


ax = plt.subplot(111, frame_on=False) # no visible frame
ax.xaxis.set_visible(False)  # hide the x axis
ax.yaxis.set_visible(False)  # hide the y axis

table(ax, df)  # where df is your data frame

plt.savefig('mytable.png')

enter image description here

1 Answers1

0

Tables are, by default, placed below the area occupied by the axes. Here you have most of the figure area occupied by an (invisible) axes, leaving little room for the table.

There are several ways to fix the issue depending on your desired output. Here, I'm using the bbox= argument of Table to override the position, and make the table occupy the entirety of the figure.

df = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
                  columns=['a', 'b', 'c'])

fig = plt.figure()
ax = plt.subplot(111)
ax.axis('off')
table(ax, df, bbox=[0,0,1,1])

enter image description here

Diziet Asahi
  • 38,379
  • 7
  • 60
  • 75