我正在尝试在IPython笔记本中格式化输出。我尝试使用to_string函数,这使我可以整洁地消除索引列。但文本数据是右对齐的。
在[10]中:
在[10]中:
import pandas as pd
columns = ['Text', 'Value']
a = pd.DataFrame ({'Text': ['abcdef', 'x'], 'Value': [12.34, 4.2]})
print (a.to_string (index=False))
Text Value
abcdef 12.34
x 4.20
当仅打印数据框时,情况也是如此。
In [12]:
print (a)
Text Value
0 abcdef 12.34
1 x 4.20
to_string函数中的justify参数令人惊讶地只对列标题进行对齐。
In [13]:
import pandas as pd
columns = ['Text', 'Value']
a = pd.DataFrame ({'Text': ['abcdef', 'x'], 'Value': [12.34, 4.2]})
print (a.to_string (justify='left', index=False))
Text Value
abcdef 12.34
x 4.20
我如何控制单个列的对齐设置?