How does Pandas DataFrame align the column name with the column data when the output is displayed?

after formatting data using pandas"s Dataframe, the output found that the column name cannot be aligned with the column data. Do you have any solutions?

clipboard.png
does not feel overwhelmed after trying to draw the table.
Please give us your advice!

Mar.09,2021

using pandas is generally recommended to use jupyter notebook


output formatting in this text format is difficult to align unless there are a small number of columns and very little data in each cell.

it is recommended to output html and view the final result through a browser.

from pandas import DataFrame
import pandas as pd
import numpy as np

df = DataFrame(np.random.randn(4, 5), columns=['A', 'B', 'C', 'D', 'E'])

print(df.to_html())
python3 pandas_html.py  > a.html && firefox a.html

final effect:


if you must face alignment under the command line, you can use terminaltables

from terminaltables import GithubFlavoredMarkdownTable

first turn dataframe into an array, then format it with GithubFlavoredMarkdownTable, and then print directly.

as to how dataframe becomes an array, you can write a simple function. For example, if the variable is df1a, use head = list (df1a) to get the title bar, nr = df1a.values.tolist () to get the data, and the combination of the two arrays will do.

Menu