2016-10-25 15 views
2

我有困難來了一個簡單的解決方案,以一個漂亮的簡單的數據幀與從文本在下面的格式轉換工作數據:在文本文件中數據幀

Dose [Gy] Relative dose [%] Structure Volume [cm³] 
      0     0     45888.7 
      0.1   0.166667     27061.7 
      0.2   0.333333     18911.6 
      0.3     0.5     14907.6 
      0.4   0.666667     12602.7 
      0.5   0.833333     11127.8 
      0.6     1     10041.9 
      0.7    1.16667     9184.75 
      0.8    1.33333     8480.96 
      0.9     1.5     7885.19 
      1    1.66667     7382.82 
      1.1    1.83333     6947.77 
      1.2     2     6570.69 
      1.3    2.16667     6242.93 
      1.4    2.33333     5959.37 
      1.5     2.5     5713.12 
      1.6    2.66667     5497.12 
      1.7    2.83333     5305.86 
      1.8     3     5135.8 
      1.9    3.16667     4983.65 
      2    3.33333     4846.38 
      2.1     3.5     4720.5 
      2.2    3.66667     4604.54 
      2.3    3.83333     4496.7 
      2.4     4     4396.11 
      2.5    4.16667     4303.21 

我在做什麼是直接索引每一行上的值,如:

for line in lines: 
     value1 = line[10:20] 
     value3 = line[55:70] 

但是,它不是很pythonic,而且根本不健壯。

現在我正試圖讓熊貓做繁重的工作,並努力讓數據正確地出來。例如:

df = pd.read_csv(StringIO.StringIO(data), sep="   ",engine='python') 

它輸出的東西仍然包含新行「\ n」和「'」以及數字。

有沒有更聰明的方法來解決這個問題?還是在大熊貓可以使用它之前需要做大量的預處理?

感謝您的任何幫助/建議!

+0

你能後的原始文本文件原樣,並不像你這樣做,因爲這混淆的事情,感謝 – EdChum

+0

完成的列表,希望那是你意思? – Ciaran

回答

1

使用read_fwf,因爲它是一個固定寬度的文件,並通過列的位置作爲元組對的列表:

In [63]: 
t=""" Dose [Gy] Relative dose [%] Structure Volume [cm³] 
      0     0     45888.7 
      0.1   0.166667     27061.7 
      0.2   0.333333     18911.6 
      0.3     0.5     14907.6 
      0.4   0.666667     12602.7 
      0.5   0.833333     11127.8 
      0.6     1     10041.9 
      0.7    1.16667     9184.75 
      0.8    1.33333     8480.96 
      0.9     1.5     7885.19 
      1    1.66667     7382.82 
      1.1    1.83333     6947.77 
      1.2     2     6570.69 
      1.3    2.16667     6242.93 
      1.4    2.33333     5959.37 
      1.5     2.5     5713.12 
      1.6    2.66667     5497.12 
      1.7    2.83333     5305.86 
      1.8     3     5135.8 
      1.9    3.16667     4983.65 
      2    3.33333     4846.38 
      2.1     3.5     4720.5 
      2.2    3.66667     4604.54 
      2.3    3.83333     4496.7 
      2.4     4     4396.11 
      2.5    4.16667     4303.21""" 

你可以看到,最終的DF是正確的格式:

df = pd.read_fwf(io.StringIO(t), colspecs=[(0,13),(14,33),(34,59)]) 
df 

Out[63]: 
    Dose [Gy] Relative dose [%] Structure Volume [cm³] 
0   0.0   0.000000    45888.70 
1   0.1   0.166667    27061.70 
2   0.2   0.333333    18911.60 
3   0.3   0.500000    14907.60 
4   0.4   0.666667    12602.70 
5   0.5   0.833333    11127.80 
6   0.6   1.000000    10041.90 
7   0.7   1.166670     9184.75 
8   0.8   1.333330     8480.96 
9   0.9   1.500000     7885.19 
10  1.0   1.666670     7382.82 
11  1.1   1.833330     6947.77 
12  1.2   2.000000     6570.69 
13  1.3   2.166670     6242.93 
14  1.4   2.333330     5959.37 
15  1.5   2.500000     5713.12 
16  1.6   2.666670     5497.12 
17  1.7   2.833330     5305.86 
18  1.8   3.000000     5135.80 
19  1.9   3.166670     4983.65 
20  2.0   3.333330     4846.38 
21  2.1   3.500000     4720.50 
22  2.2   3.666670     4604.54 
23  2.3   3.833330     4496.70 
24  2.4   4.000000     4396.11 
25  2.5   4.166670     4303.21 
2

我認爲你需要分隔符s{2,} - 2個或更多空格:

import pandas as pd 
import numpy as np 
from pandas.compat import StringIO 

temp=u"""Dose [Gy] Relative dose [%] Structure Volume [cm³] 
      0     0     45888.7 
      0.1   0.166667     27061.7 
      0.2   0.333333     18911.6 
      0.3     0.5     14907.6 
      0.4   0.666667     12602.7 
      0.5   0.833333     11127.8 
      0.6     1     10041.9 
      0.7    1.16667     9184.75 
      0.8    1.33333     8480.96 
      0.9     1.5     7885.19 
      1    1.66667     7382.82 
      1.1    1.83333     6947.77 
      1.2     2     6570.69 
      1.3    2.16667     6242.93 
      1.4    2.33333     5959.37 
      1.5     2.5     5713.12 
      1.6    2.66667     5497.12 
      1.7    2.83333     5305.86 
      1.8     3     5135.8 
      1.9    3.16667     4983.65 
      2    3.33333     4846.38 
      2.1     3.5     4720.5 
      2.2    3.66667     4604.54 
      2.3    3.83333     4496.7 
      2.4     4     4396.11 
      2.5    4.16667     4303.21""" 
#after testing replace StringIO(temp) to filename 
df = pd.read_csv(StringIO(temp),sep=r'\s{2,}', engine='python') 
print (df) 
    Dose [Gy] Relative dose [%] Structure Volume [cm³] 
0   0.0   0.000000    45888.70 
1   0.1   0.166667    27061.70 
2   0.2   0.333333    18911.60 
3   0.3   0.500000    14907.60 
4   0.4   0.666667    12602.70 
5   0.5   0.833333    11127.80 
6   0.6   1.000000    10041.90 
7   0.7   1.166670     9184.75 
8   0.8   1.333330     8480.96 
9   0.9   1.500000     7885.19 
10  1.0   1.666670     7382.82 
11  1.1   1.833330     6947.77 
12  1.2   2.000000     6570.69 
13  1.3   2.166670     6242.93 
14  1.4   2.333330     5959.37 
15  1.5   2.500000     5713.12 
16  1.6   2.666670     5497.12 
17  1.7   2.833330     5305.86 
18  1.8   3.000000     5135.80 
19  1.9   3.166670     4983.65 
20  2.0   3.333330     4846.38 
21  2.1   3.500000     4720.50 
22  2.2   3.666670     4604.54 
23  2.3   3.833330     4496.70 
24  2.4   4.000000     4396.11 
25  2.5   4.166670     4303.21 
0

雖然其他解決方案可能更pythonic,我會建議先轉換文件,使它不再包含多個空格。然後你就可以輕鬆地將其讀入數據幀大熊貓:

import pandas as pd 

infile = open('test.txt', 'r') 
outfile = open('testout.txt', 'w') 

for eachrow in infile: 
    stripped = '#'.join(filter(None,eachrow.split(' '))) 
    outfile.write(stripped) 

infile.close() 
outfile.close() 

df = pd.read_csv('testout.txt', encoding = 'latin1', sep='#', engine='python') 
df.head()