简体   繁体   English

打印 Pandas 系列与其他字符串打印时垂直对齐

[英]Print Pandas Series vertically aligned when printed with another string

I have a Pandas Series, which should be printed with a string.我有一个 Pandas 系列,应该用字符串打印。 The result of the following code does not look pretty.以下代码的结果看起来并不漂亮。

series1 = pd.Series({'a':1, 'b':2})
print(f"The series is: {series1}")

Result:结果:

The series is: a    1
b    2

What I want:我想要的是:

The series is: a    1
               b    2

Would it be possible with a simple code?有没有可能用一个简单的代码?

print(f"The series is:\n{series1}")

May be a possible solution, but not perfectly what I want.可能是一个可能的解决方案,但不完全是我想要的。

Try adding 15 spaces to the rest of the lines, not the first line:尝试在行的 rest 中添加 15 个空格,而不是第一行:

s = series1.to_string()
line1, *rest = s.splitlines()
s = '\n'.join([line1, *map((' ' * 15).__add__, rest)])
print(f"The series is: {s}")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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