在Pandas DataFrame输出中对齐列

时间:2018-08-07 14:28:15

标签: python-3.x

我生成了一个列表,并将其转换为Pandas DataFrame。列表在df中大约为24500行。但是我无法对齐CSV文件的倾斜列或复制到文本编辑器,这是一段代码片段,可从大型数据集中输出五个数字的组合。有没有一种方法可以格式化DataFrame中完全对齐的列?

combo = sorted(counter.items(), key=lambda value: value[1], reverse=True)
print(type(combo))
xlist = combo
arr1 = np.array(xlist)
df1 = pd.DataFrame(arr1)
print(df1)   

这是DataFrame输出的一小部分;

Total number of lines: 3605
<class 'list'>
                          0  1
0        (3, 9, 10, 13, 20)  2
1       (33, 38, 7, 42, 21)  2
2       (34, 37, 39, 8, 47)  2
3       (35, 36, 5, 10, 45)  2
4      (44, 14, 47, 25, 31)  2
5      (39, 49, 21, 24, 25)  2
6        (32, 1, 3, 47, 19)  2
7       (3, 35, 17, 20, 30)  2
8       (2, 41, 42, 44, 47)  2
9      (40, 10, 11, 16, 28)  2

In answer to your question my desired output would be to have the columns aligned as below;

0   3   9   10  13  20  2
1   33  38  7   42  21  2
2   34  37  39  8   47  2
3   35  36  5   10  45  2
4   44  14  47  25  31  2
5   39  49  21  24  25  2
6   32  1   3   47  19  2
7   3   35  17  20  30  2
8   2   41  42  44  47  2
9   40  10  11  16  28  2
10  37  9   46  27  28  2

1 个答案:

答案 0 :(得分:0)

您可以使用熊猫的to_string

arr1 = np.random.randint(0, 50, 60).reshape(10, 6)
arr1[:, -1] = 2
df1 = pd.DataFrame(arr1)
print(df1.to_string(header=False))

>>>
0  37  39  34  18  33  2
1  18  44  40  48  28  2
2  24   2   9  25  13  2
3  43   8   2   8  42  2
4  31  32  22  10   4  2
5  20  10  20   7   6  2
6  34  17  21   5  39  2
7  33  21  21  41  41  2
8  26  30  41  48  18  2
9  37  38  24  19  15  2