我想将数据框的列名包装为特定的宽度。例如,变换
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
0 -1.526078 0.258110
1 0.263490 -0.268635
2 -1.264121 -1.257454
类似
aaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaa
aaaaaaaaaaaaa aaaaaaaaaaaaa
0 -1.526078 0.258110
1 0.263490 -0.268635
2 -1.264121 -1.257454
此stackoverflow post中有解决此问题的方法,但它仅适用于jupyter笔记本,不适用于常规python。
用于重现数据帧的代码:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(3, 2), columns=['a' * 30] * 2)
print(df)