简体   繁体   中英

Wrapping column name of pandas dataframe

I would like to wrap the column names of a data frame to some specific width. For instance, transform

   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
0                       -1.526078                        0.258110
1                        0.263490                       -0.268635
2                       -1.264121                       -1.257454

to something like

   aaaaaaaaaaaaaaaaa  aaaaaaaaaaaaaaaaa
    aaaaaaaaaaaaa      aaaaaaaaaaaaa
0      -1.526078         0.258110
1       0.263490        -0.268635
2      -1.264121        -1.257454

There is a solution to this problem in this stackoverflow post , but it only works in jupyter notebooks, not in regular python.

Code to reproduce the dataframe:

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(3, 2), columns=['a' * 30] * 2)
print(df)

您可以将DataFrame包装在自己的类中,该类将重写__str__和/或__repr__方法以实现此目的。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM