我有5000行數據,看起來像在csv文件中的以下內容,我想按最後一列6(即A,B)使用numpy數組,因爲之後我會在每個組中繪製數據。Python numpy按字符串列的值拆分csv文件
Title
Date, Time, Value1, Value2, Value3, Value4, Value5
,, Unit1, Unit2, Unit3,,
2012-04-02,00:00, 85.5333333333333, 4.87666666666667, 8.96, 323.27,A
2012-04-02,00:30, 196.5, 5.49, 8.42, 323.15,B
2012-04-02,01:00, 68.2, 4.47, 7.83, 325.30,A
2012-04-02,01:30, 320.9, 6.77333333333333, 8.05, 326.63,B
我不得不當我加載與np.genfromtxt的數據,或者指定D型=無的一個術語變成NaN How to use numpy.genfromtxt when first column is string and the remaining columns are numbers?
我想使用itertools GROUPBY返回基於所有值在最後一列,在這裏提到:How do I use Python's itertools.groupby()? 但首先,我需要排序numpy數組。
我試圖使用高級索引,通過拼接第六列並對其進行排序 Python (Numpy) array sorting 即, v [v,0] .argsort()]
但是,這裏是一個鏈接,提到numpy會把我的記錄當作我的dtype(它被設置爲none)的一維數組,並且我碰到相同的索引錯誤嘗試這種排序:我怎麼能分裂numpy的陣列使用了基於第6列的字符串值GROUPBY爲了分別繪製他們
1): Numpy Array Column Slicing Produces IndexError: invalid index Exception
問題?
2)如果能夠跳過第一行(標題)和第三行(單位)並且保留第二行(列標題)和數據,那麼也很好。任何人都知道如何使用可用選項輕鬆完成該任務?
這是劇本我到目前爲止,:
import numpy as np
from matplotlib import pyplot as plt
from itertools import groupby
import csv
regression_data_dp1 = np.genfromtxt(「file.csv」, delimiter=',', skiprows=3, dtype=None)
sortindex = regression_data_dp1[:,6]
#Error is hit at this step:
# sortindex = regression_data_dp1[:,6]
#IndexError: invalid index
regression_data_dp1_sorted = regression_data_dp1[ regression_data_dp1(:,column_WRF_wind_direction).argsort()]
for key, group in groupby(regression_data_dp1, lambda x: x[0]):
print key
with open(「file_" + key.strip() + ".csv", 'w') as data_file:
wr=csv.writer(data_file, quoting=csv.QUOTE_ALL)
for item in (group):
wr.writerow(item)
什麼是標題?你有比數據字段更多的標題;這是數據文件的樣子嗎? – dawg
@drewk標題只是對文件ie的描述。 「這是......等的報告」。第二行是標題,有7個標題。下一行是單位。然後數據也有7列。 – frank