Print Pandas Series vertically aligned when printed with another string

Viewed 125

I have a Pandas Series, which should be printed with a string. 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.

1 Answers

Try adding 15 spaces to the rest of the lines, not the first line:

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