Python 3.x 对齐数据帧输出中的列
我生成了一个列表,并将其转换为数据帧。df中的列表约为24500行。但我无法将csv文件的倾斜列对齐或复制到文本编辑器。有没有一种方法可以格式化数据框中完全对齐的列Python 3.x 对齐数据帧输出中的列,python-3.x,Python 3.x,我生成了一个列表,并将其转换为数据帧。df中的列表约为24500行。但我无法将csv文件的倾斜列对齐或复制到文本编辑器。有没有一种方法可以格式化数据框中完全对齐的列 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)
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)
这是数据帧输出的一小部分
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
总行数:3605行
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
在回答你的问题时,我想要的输出是将列对齐,如下所示;
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
您可以使用panda的:
能否请您提供一个预期输出的示例(例如,对于您提供的数据片段)?请查看我的原始文档中添加的注释,因为我在此处粘贴所需输出时遇到问题
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