2016-11-22 60 views
0

我從github獲得一個數據csv文件並使用pd.csv_read()來讀取它。它會自動創建這樣的系列號碼。如何在csv_read()時自動創建序列號?

label repeattrips  id offer_id never_bought_company \ 
0  1   5  86246 1208251      0 
1  1   16  86252 1197502      0 
2  0   0 12682470 1197502      1 
3  0   0 12996040 1197502      1 
4  0   0 13089312 1204821      0 
5  0   0 13179265 1197502      1 
6  0   0 13251776 1200581      0 

但是當我創建我的csv文件並閱讀它。

label gender age_range action0 action1 action2 action3 first \ 
0  0  2   1  0  1  0  2  1 
0  0  4   0  0  1  0  1  1 
0  1  2   8  0  1  0  9  1 
1  0  2   0  0  1  0  1  1 
0  1  5   0  0  1  0  1  1 
0  1  5   0  0  1  0  1  1 

該標籤在我的輸出中被視爲系列號。

如果我在每行數據的前面創建一個序列號,仍然沒有解決問題。像這樣:

 label gender age_range action0 action1 action2 action3 first \ 
0 0  0  2   1  0  1  0  2  1 
1 0  0  4   0  0  1  0  1  1 
2 0  1  2   8  0  1  0  9  1 
3 1  0  2   0  0  1  0  1  1 
4 0  1  5   0  0  1  0  1  1 
5 0  1  5   0  0  1  0  1  1 
6 0  0  7   5  0  1  0  6  1 
7 0  0  7   1  0  1  0  2  1 

我不知道我是否保存了它的權利。我的CSV數據是這樣的(添加序列號)和GitHub的文件看起來相似的格式,以及:

label gender age_range action0 action1 action2 action3 first second third fourth sirstrate secondrate thirdrate fourthrate total_cat total_brand total_time total_items users_appear users_items users_cats users_brands users_times users_action0 users_action1 users_action2 users_action3 merchants_appear merchants_items merchants_cats merchants_brands merchants_times merchants_action0 merchants_action1 merchants_action2 merchants_action3 
0 0 0 2 1 0 1 0 2 1 1 0 0.0224719101124 0.5 0.5 0 1 1 1 1 89 71 22 45 17 87 0 2 0 46 34 11 16 3 38 4 2 2 
1 0 0 4 0 0 1 0 1 1 1 0 0.00469483568075 0.0232558139535 0.0232558139535 0.0 1 1 1 1 213 102 47 44 30 170 0 36 7 103 58 25 23 6 81 0 22 0 
2 0 1 2 8 0 1 0 9 1 1 0 0.0157342657343 0.0181818181818 0.0181818181818 0.0 2 2 1 5 572 393 111 158 60 517 0 15 40 119 70 24 20 17 106 6 7 0 
3 1 0 2 0 0 1 0 1 1 1 0 0.0142857142857 0.0769230769231 0.0769230769231 0.0 1 1 1 1 70 33 19 15 15 57 0 11 2 27 17 11 15 11 18 0 2 7 
4 0 1 5 0 0 1 0 1 1 1 0 0.025641025641 0.2 0.2 0.0 1 1 1 1 39 32 16 29 14 34 0 4 1 133 88 26 25 11 128 0 5 0 

一個在一行空白,而不是一個空一行的每一個項目。

你能告訴我如何解決這個問題嗎?

回答

1

您需要提供代碼才能獲得更多實質性幫助,因爲您不清楚爲什麼會遇到問題。例如,複製您在底部粘貼的數據只是罰款與pd.read_clipboard()讀取和pd.read_csv()應該也蠻好用空格分隔,只要您設置工作:

In [2]: pd.read_clipboard() 
Out[2]: 
    label gender age_range action0 action1 action2 action3 first \ 
0  0  0   2  1  0  1  0  2 
1  0  0   4  0  0  1  0  1 
2  0  1   2  8  0  1  0  9 
3  1  0   2  0  0  1  0  1 
4  0  1   5  0  0  1  0  1 

    second third  ...   users_action3 merchants_appear \ 
0  1  1  ...      0    46 
1  1  1  ...      7    103 
2  1  1  ...      40    119 
3  1  1  ...      2    27 
4  1  1  ...      1    133 

    merchants_items merchants_cats merchants_brands merchants_times \ 
0    34    11    16    3 
1    58    25    23    6 
2    70    24    20    17 
3    17    11    15    11 
4    88    26    25    11 

    merchants_action0 merchants_action1 merchants_action2 merchants_action3 
0     38     4     2     2 
1     81     0     22     0 
2    106     6     7     0 
3     18     0     2     7 
4    128     0     5     0 

[5 rows x 37 columns] 
+0

我發現我的每一行的末端'+」, '+' \ n''。這就是大聲笑的原因 – fourth