2015-11-29 97 views
3

無法將以下空格分隔的數據文件導入python並將它們拆分爲可以使用的數據框。 原始數據文件是這樣的:將帶空格分隔符的文本文件導入python中的csv

3300 0.272 0.302 69 153 21 4 31 104 22 80 4 3 1 0 0 0 "Andre Dawson  " 
2600 0.269 0.335 58 111 17 2 18 66 39 69 0 3 1 1 0 0 "Steve Buchele " 

import pandas as pd 
data = pd.read_csv('../data/ABRMdata', header=None) 
split_text = [] 
for line in data: 
    split_text.append(line) 

return split_text 

,我只得到[0]返回 但我希望列表

[3300,0.272,0.302,69,153,21,4,31,104,22,80,4,3,1, 0,0,0,"Andre Dawson "] 
[2600,0.269,0.335,58,111,17,2,18,66,39,69, 0,3,1,1,0,0,"Steve Buchele "] 

任何想法列表返回的數據? 感謝您的高級幫助

回答

1

你可以使用sep參數大熊貓read_csv的:

import pandas as pd 
from io import StringIO 

data = """ 
3300 0.272 0.302 69 153 21 4 31 104 22 80 4 3 1 0 0 0 "Andre Dawson  " 
2600 0.269 0.335 58 111 17 2 18 66 39 69 0 3 1 1 0 0 "Steve Buchele " 
""" 

df = pd.read_csv(StringIO(data), sep='\s+', header=None) 


In [87]: df 
Out[87]: 
    0  1  2 3 4 5 6 7 8 9 10 11 12 13 14 15 \ 
0 3300 0.272 0.302 69 153 21 4 31 104 22 80 4 3 1 0 0 
1 2600 0.269 0.335 58 111 17 2 18 66 39 69 0 3 1 1 0 

    16     17 
0 0 Andre Dawson  
1 0 Steve Buchele 

編輯

,或者你可以設置delim_whitespaceTrue

df = pd.read_csv(StringIO(data), delim_whitespace=True, header=None) 

In [440]: df 
Out[440]: 
    0  1  2 3 4 5 6 7 8 9 10 11 12 13 14 15 \ 
0 3300 0.272 0.302 69 153 21 4 31 104 22 80 4 3 1 0 0 
1 2600 0.269 0.335 58 111 17 2 18 66 39 69 0 3 1 1 0 

    16     17 
0 0 Andre Dawson 
1 0 Steve Buchele 
+0

工作就像一種享受。謝謝 – pakkunrob

+1

@pakkunrob不要忘記,如果你滿意答案,你可以upvote和接受它。謝謝 –

1

您需要使用熊貓嗎?

這段代碼會讓你在熊貓之外開始。 (它不是糾正您的具體要求)

import csv 

with open('/Users/toasteez/desktop/file.txt', 'r') as csvfile: 
    w = csv.reader(csvfile) 
    for line in w: 
     newline = str.replace(line[0],' ',',') 
     print(newline) 
1

正如David提到大熊貓read_fwf可用於創建數據幀可以使用to_dict()和許多其他數據結構轉換爲python字典。

In [30]: df = pd.read_fwf("filefor",header=None) 

In [31]: df 
Out[31]: 
    0  1  2 3 4 5 6 7 8 9 10 11 12 13 14 15 \ 
0 3300 0.272 0.302 69 153 21 4 31 104 22 80 4 3 1 0 0 
1 2600 0.269 0.335 58 111 17 2 18 66 39 69 0 3 1 1 0 

    16  17  18 19 
0 0 "Andre Dawson " 
1 0 "Steve Buchele " 

In [32]: df.to_dict() 
Out[32]: 
{0: {0: 3300, 1: 2600}, 
1: {0: 0.27200000000000002, 1: 0.26899999999999996}, 
2: {0: 0.30199999999999999, 1: 0.33500000000000002}, 
3: {0: 69, 1: 58}, 
4: {0: 153, 1: 111}, 
5: {0: 21, 1: 17}, 
6: {0: 4, 1: 2}, 
7: {0: 31, 1: 18}, 
8: {0: 104, 1: 66}, 
9: {0: 22, 1: 39}, 
10: {0: 80, 1: 69}, 
11: {0: 4, 1: 0}, 
12: {0: 3, 1: 3}, 
13: {0: 1, 1: 1}, 
14: {0: 0, 1: 1}, 
15: {0: 0, 1: 0}, 
16: {0: 0, 1: 0}, 
17: {0: '"Andre', 1: '"Steve'}, 
18: {0: 'Dawson', 1: 'Buchele'}, 
19: {0: '"', 1: '"'}} 

是它infered空間插圖中最後一個字段作爲分隔符,以避免widths=[1,5....]可以用來

其他DS爲使用

df.to_clipboard df.to_hdf  df.to_period  df.to_string 
df.to_csv  df.to_html  df.to_pickle  df.to_timestamp 
df.to_dense  df.to_json  df.to_records df.to_wide 
df.to_dict  df.to_latex  df.to_sparse  
df.to_excel  df.to_msgpack df.to_sql   
df.to_gbq  df.to_panel  df.to_stata 
相關問題