熊猫:将数据解析为列

时间:2018-10-15 00:29:07

标签: python pandas

以下是我的代码

import pandas as pd
pd.set_option('display.max_rows',50)     #Extending the amout of viewable rows
pd.set_option('display.max_colwidth',100) #extending the amount of viewable column width


col = ["bytes",'N',"type","root","time","algbw","busbw","delta"]
df = pd.read_table('test-pairs.txt',header=None,error_bad_lines = False, 
comment = '#')#,skiprows=Skip) #, squeeze=False )
print df

以下是我的结果

                                                                           0
0                                                                      0 - 1
1     2000000000    2000000000    char     0   88.307  22.65  22.65    0e+00
2     2000000000    2000000000    char     1   87.351  22.90  22.90    0e+00
3                                                                      0 - 2
4     2000000000    2000000000    char     0   88.328  22.64  22.64    0e+00
5     2000000000    2000000000    char     1   87.343  22.90  22.90    0e+00
6                                                                      0 - 3
7     2000000000    2000000000    char     0   78.093  25.61  25.61    0e+00
8     2000000000    2000000000    char     1   78.203  25.57  25.57    0e+00
9                                                                      0 - 4
10    2000000000    2000000000    char     0   78.042  25.63  25.63    0e+00
11    2000000000    2000000000    char     1   76.976  25.98  25.98    0e+00
12                                                                     0 - 5
13    2000000000    2000000000    char     0  220.868   9.06   9.06    0e+00
14    2000000000    2000000000    char     1  205.188   9.75   9.75    0e+00
15                                                                     0 - 6
16    2000000000    2000000000    char     0  217.028   9.22   9.22    0e+00
17    2000000000    2000000000    char     1  214.294   9.33   9.33    0e+00
18                                                                     0 - 7
19    2000000000    2000000000    char     0  217.843   9.18   9.18    0e+00
20    2000000000    2000000000    char     1  205.845   9.72   9.72    0e+00
21                                                                     1 - 2
22    2000000000    2000000000    char     0   87.428  22.88  22.88    0e+00
23    2000000000    2000000000    char     1   78.064  25.62  25.62    0e+00 
24                                                                     1 - 3
..                                                                       ...
59    2000000000    2000000000    char     1  208.865   9.58   9.58    0e+00
60                                                                     3 - 6
61    2000000000    2000000000    char     0  214.659   9.32   9.32    0e+00
62    2000000000    2000000000    char     1  214.902   9.31   9.31    0e+00
63                                                                     3 - 7
64    2000000000    2000000000    char     0   87.359  22.89  22.89    0e+00
65    2000000000    2000000000    char     1   87.346  22.90  22.90    0e+00
66                                                                     4 - 5
67    2000000000    2000000000    char     0   87.767  22.79  22.79    0e+00
68    2000000000    2000000000    char     1   87.355  22.90  22.90    0e+00
69                                                                     4 - 6
70    2000000000    2000000000    char     0   87.803  22.78  22.78    0e+00
71    2000000000    2000000000    char     1   87.343  22.90  22.90    0e+00
72                                                                     4 - 7
73    2000000000    2000000000    char     0   76.989  25.98  25.98    0e+00
74    2000000000    2000000000    char     1   77.033  25.96  25.96    0e+00
75                                                                     5 - 6
76    2000000000    2000000000    char     0   88.580  22.58  22.58    0e+00
77    2000000000    2000000000    char     1   77.140  25.93  25.93    0e+00
78                                                                     5 - 7
79    2000000000    2000000000    char     0   87.508  22.86  22.86    0e+00
80    2000000000    2000000000    char     1   87.375  22.89  22.89    0e+00
81                                                                     6 - 7
82    2000000000    2000000000    char     0   87.305  22.91  22.91    0e+00
83    2000000000    2000000000    char     1   76.999  25.97  25.97    0e+00

您好,我正在将Pandas与Python配合使用。我试图解析列,并尝试从代码列表中将列命名为“ col”。我尝试使用sep =“”和sep =“ \ s +”,但是这些对我不起作用。我想将诸如0-1,0-2,0-3 ... 6-7之类的索引保留为列表。有什么办法去做吗?任何帮助,将不胜感激。如果此信息相关,则为Idk,但在编译代码时显示[84行x 1列]。

1 个答案:

答案 0 :(得分:0)

test-pairs.txt 文件中的列如何分隔?从问题陈述中,我假设它们由制表符或空格分隔。尝试以下方法,

df = pd.read_table('test-pairs.txt', header=None, names=col, sep='\s+')

要对行进行索引的方式真的很难理解。您能否通过一些示例详细说明一下输入是什么以及期望输出是什么?