我试图在IPython笔记本中格式化输出.我尝试使用to_string函数,这巧妙地让我删除了索引列.但文本数据是正确的.
在[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
Run Code Online (Sandbox Code Playgroud)
仅打印数据帧时也是如此.
在[12]中:
print (a)
Text Value
0 abcdef 12.34
1 x 4.20
Run Code Online (Sandbox Code Playgroud)
令人惊讶的是,to_string函数中的justify参数只能证明列标题的合理性.
在[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
Run Code Online (Sandbox Code Playgroud)
如何控制各列的对齐设置?