2016-01-30 83 views
0

我有一個包含表格格式的數值數據的文件,它總共有6列。我想要讀取5維數組中的5列值,並將其存儲到另一個數組中。 我試圖使用的loadtxt(),但它將所有列加載到一個。即使我試圖使用loadtext()unpack參數,但它將每列存儲到不同的變量。 我搜索了「stackoverflow」爲同一類型的問題,但沒有找到任何東西。 下面是我的數據使用python讀取表格數據

5000 0 0.3048 71.3 0.00266337 121.301 
6300 0 0.3048 71.3 0.00266337 119.541 
8000 0 0.3048 71.3 0.00266337 117.151 
10000 0 0.3048 71.3 0.00266337 115.391 
12500 0 0.3048 71.3 0.00266337 112.241 
16000 0 0.3048 71.3 0.00266337 108.721 
+1

你可以使用'pandas''read_table'函數 –

回答

0

的使用pandasread_csvilocvalues方法例如:

import pandas as pd 
from io import StringIO 

data =""" 
5000 0 0.3048 71.3 0.00266337 121.301 
6300 0 0.3048 71.3 0.00266337 119.541 
8000 0 0.3048 71.3 0.00266337 117.151 
10000 0 0.3048 71.3 0.00266337 115.391 
12500 0 0.3048 71.3 0.00266337 112.241 
16000 0 0.3048 71.3 0.00266337 108.721 
""" 

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

a = df.iloc[:, :5].values 
b = df.iloc[:, 5].values 

In [192]: a 
Out[192]: 
array([[ 5.00000000e+03, 0.00000000e+00, 3.04800000e-01, 
      7.13000000e+01, 2.66337000e-03], 
     [ 6.30000000e+03, 0.00000000e+00, 3.04800000e-01, 
      7.13000000e+01, 2.66337000e-03], 
     [ 8.00000000e+03, 0.00000000e+00, 3.04800000e-01, 
      7.13000000e+01, 2.66337000e-03], 
     [ 1.00000000e+04, 0.00000000e+00, 3.04800000e-01, 
      7.13000000e+01, 2.66337000e-03], 
     [ 1.25000000e+04, 0.00000000e+00, 3.04800000e-01, 
      7.13000000e+01, 2.66337000e-03], 
     [ 1.60000000e+04, 0.00000000e+00, 3.04800000e-01, 
      7.13000000e+01, 2.66337000e-03]]) 

In [193]: b 
Out[193]: array([ 121.301, 119.541, 117.151, 115.391, 112.241, 108.721]) 
0

如果你想使用NumPy的沒有大熊貓的解決方案,可以閱讀在整個數據和然後使用np.hsplit

In [75]: data = np.loadtxt('data.txt') 

In [76]: d, e = np.hsplit(data, (-1,))  # split at the final column 

In [77]: d 
Out[77]: 
array([[ 5.00000000e+03, 0.00000000e+00, 3.04800000e-01, 
      7.13000000e+01, 2.66337000e-03], 
     [ 6.30000000e+03, 0.00000000e+00, 3.04800000e-01, 
      7.13000000e+01, 2.66337000e-03], 
     [ 8.00000000e+03, 0.00000000e+00, 3.04800000e-01, 
      7.13000000e+01, 2.66337000e-03], 
     [ 1.00000000e+04, 0.00000000e+00, 3.04800000e-01, 
      7.13000000e+01, 2.66337000e-03], 
     [ 1.25000000e+04, 0.00000000e+00, 3.04800000e-01, 
      7.13000000e+01, 2.66337000e-03], 
     [ 1.60000000e+04, 0.00000000e+00, 3.04800000e-01, 
      7.13000000e+01, 2.66337000e-03]]) 

In [78]: e 
Out[78]: 
array([[ 121.301], 
     [ 119.541], 
     [ 117.151], 
     [ 115.391], 
     [ 112.241], 
     [ 108.721]])