如何在 python 中对列表进行分栏?
How to columnate a list in python?
我有一个 pandas Series
,足够小,所以我可以阅读所有项目(大约 80 个),但对于屏幕尺寸来说太大了。
是否有一些 python
命令或 ipython
(笔记本)魔术命令或 pandas
函数来列化 list
或 Series
以便我可以阅读完全不需要上下滚动?
我基本上是在 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
以下non-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
使用 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
的内容。
我有一个 pandas Series
,足够小,所以我可以阅读所有项目(大约 80 个),但对于屏幕尺寸来说太大了。
是否有一些 python
命令或 ipython
(笔记本)魔术命令或 pandas
函数来列化 list
或 Series
以便我可以阅读完全不需要上下滚动?
我基本上是在 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
以下non-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
使用 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
的内容。