'How do I format a list a column?
I need to format a list into a column.
a = [1,2,3,4,5]
The desired output:
a
0 1
1 2
2 3
3 4
4 5
Solution 1:[1]
You can use str.format:
a = [1, 2, 3, 4, 5]
print("{:<10} {:<10}".format("", "a"))
for i, v in enumerate(a):
print("{:<10} {:<10}".format(i, v))
Prints:
a
0 1
1 2
2 3
3 4
4 5
Or making a DataFrame:
import pandas as pd
df = pd.DataFrame(a, columns=["a"])
print(df)
Prints:
a
0 1
1 2
2 3
3 4
4 5
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andrej Kesely |
