简体   繁体   English

如何在python3中的表中水平打印列表,这与通常方式相反?

[英]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? 如何在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. 我想要的是将这些列表水平添加为“ a”作为列,将“ b”添加为另一个列,并将所有其他列表作为单独的列。

how to achieve this in python, I tried texttable, tabulate, but no luck they won't allow to create table horizontally 如何在python中实现这一点,我尝试了texttable,制表,但是幸运的是,他们不允许水平创建表

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: 那么df等于:

     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. 我不确定“将所有列表作为单独的列”是什么意思,但是我不建议将列表放在DataFrame的单元格中。

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

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