chiju
chiju

Reputation: 27

how to print lists horizontally in a table in python3 opposite to the usual way?

how to print lists horizontally in a table in python3? for eg

lists:

a=[1,2,4,6]
b=[2,4,5,6]
c=[4,5,86,6]
d=[3,4,6]

what I want is horizontally add these lists as 'a' as column and 'b' as another colomn and all the rest of the lists as separate columns.

how to achieve this in python, I tried texttable, tabulate, but no luck they won't allow to create table horizontally

Upvotes: 0

Views: 738

Answers (1)

Henry Woody
Henry Woody

Reputation: 15662

You can use pandas to do this pretty simply. Pandas is also a great library for working with dataframes/tables in general.

Here's how you can read your data in as columns:

import pandas as pd

a=[1,2,4,6]
b=[2,4,5,6]
c=[4,5,86,6]
d=[3,4,6]

df = pd.DataFrame([a,b,c,d]).transpose()
df.columns = ['a', 'b', 'c', 'd']

Then df is equal to:

     a    b     c    d
0  1.0  2.0   4.0  3.0
1  2.0  4.0   5.0  4.0
2  4.0  5.0  86.0  6.0
3  6.0  6.0   6.0  NaN

I'm not sure what you mean by "all the lists as separate column" but I wouldn't recommend putting lists in cells in a DataFrame.

Upvotes: 1

Related Questions