【问题标题】:How to columnate a list in python?如何在python中对列表进行分栏?
【发布时间】:2015-08-11 14:33:04
【问题描述】:

我有一个pandas Series,足够小,我可以阅读所有项目(大约 80 个),但对于屏幕尺寸来说太大了。

是否有一些python 命令或ipython(笔记本)魔术命令或pandas 函数来列化listSeries,这样我就可以完全阅读它而无需向下和向上滚动?

我基本上是在bash 中寻找与column 命令等效的命令。

例如,我有一个这样的Series

A      76
B      56
C      42
D      31
E      31
F      25
G      24

假设我的屏幕很小,我需要向下滚动才能看到B 行之后。我想要的是这样的:

A    76        C    42        E    31        G    24
B    56        D    31        F    25

【问题讨论】:

  • 看看那个问题和接受的答案:stackoverflow.com/questions/11707586/…这是你想做的吗?
  • 不,这是我现在正在做的,以显示超过 X 行。问题是我有 1 列,我希望它显示为多列,以便所有数据都可以在屏幕中显示。
  • 什么是“屏幕”?是终端吗?它是 HTML 吗?终端中的列数是否始终保持一致?
  • 屏幕是终端窗口或ipython笔记本页面的大小。
  • 我不确定,但我会考虑寻找 numpy 的解决方案,因为 numpy 输出确实更倾向于这种输出风格(尽管显然它缺少的是索引)

标签: python pandas ipython multiple-columns


【解决方案1】:

以下非 numpy/pandas 解决方案可能会给您一些启发:

import itertools

rows =  [("A", 76),  ("B", 56), ("C", 42), ("D", 31), ("E", 31), ("F", 25), ("G", 24)]

def print_multi_cols(lrows, split_at=5, space_each=4):
    for row in itertools.izip(*itertools.izip_longest(*[iter(lrows)]*split_at, fillvalue=(" ", " "))):
        for col in row:
            print " ".join(["%-*s" % (space_each, item) for item in col]),
        print

print_multi_cols(rows, 2)
print_multi_cols(rows, 3)

这给出了以下输出:

A     76      C     42      E     31      G     24     
B     56      D     31      F     25                   

A     76      D     31      G     24     
B     56      E     31                   
C     42      F     25

您需要先转换您的系列,然后才能使用它。使用 Python 2.7 测试。

或者为了更好地控制理由,可以修改如下:

import itertools

rows =  [("A", 9),  ("B", 56), ("C", 42), ("D", 31), ("E", 31), ("F", 25), ("G", 999)]

def print_multi_cols(lrows, split_at=5, space_each=4, left_justify=None):
    if not left_justify:
        left_justify = [True] * len(lrows[0])

    for row in itertools.izip(*itertools.izip_longest(*[iter(lrows)]*split_at, fillvalue=(" ", " "))):
        for col in row:
            print " ".join([("%-*s " if left else "%*s ") % (space_each, item) for item, left in itertools.izip(col, left_justify)]),
        print
    print

print_multi_cols(rows, split_at=5, space_each=3, left_justify=[True, False])

给予:

A      9  F     25 
B     56  G    999 
C     42           
D     31           
E     31

【讨论】:

  • 谢谢,它看起来是我想要的。要传递Series 对象,您需要执行my_series.reset_index().values。可能有更好的方法来通过系列,但我找不到。无论如何,谢谢,如果有人提出其他解决方案,我会等待。
【解决方案2】:

使用show 函数的想法(有一些循环,但由于可以这样显示的数据很小,应该没问题)。

def show(series, cols=6):                                                        
    rows = int(np.ceil(len(series)/float(cols)))                                 
    indices = series.index                                                       

    ind_loop = 0                                                                 
    for row in range(rows):                                                      
        ind = indices[ind_loop:ind_loop+cols]                                    
        dat = series[ind]                                                        
        comb = zip(ind, dat)                                                     
        print_str = ""                                                           
        for num in range(len(dat)):                                              
            print_str += "{{{0}: <10}} ".format(num)                             

        print(print_str.format(*comb))                                           
        ind_loop += cols                                                         

ser = pd.Series(range(20))                                                                                                                                                        
show(ser, cols=6) 

(0, 0)     (1, 1)     (2, 2)     (3, 3)     (4, 4)     (5, 5)                     
(6, 6)     (7, 7)     (8, 8)     (9, 9)     (10, 10)   (11, 11)
(12, 12)   (13, 13)   (14, 14)   (15, 15)   (16, 16)   (17, 17)
(18, 18)   (19, 19)

如果您愿意,可以调整 print 以显示类似 index : value 的内容。

【讨论】:

    猜你喜欢
    • 2011-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多