# 讀取.csv.格式資料 把日期和時間兩列合并
csv_data = pd.read_csv(path, parse_dates={'date_time': ['date', 'time']})
# 利用dataframe保存資料資料
csv_data = pd.DataFrame(csv_data, columns=['ID', 'longitude', 'latitude', 'date_time'])
# 排序(按ID號與時間順序)
csv_data = csv_data.sort_values(by=['ID', 'date_time'])
# 重新設定索引
csv_data = csv_data.reset_index(drop=True)
# 選擇前十行
csv_data = csv_data[0:10]
csv_data.drop([0], inplace=True)
csv_data = csv_data.reset_index(drop=True)
print(csv_data)
csv_data資料如下:
ID longitude latitude date_time
0 D3410D9N7C00184Q 116.403412 39.924820 2017-05-02 09:06:36
1 D3410D9N7C00184Q 116.404114 39.924870 2017-05-02 09:06:56
2 D3410D9N7C00184Q 116.404663 39.924999 2017-05-02 09:07:06
3 D3410D9N7C00184Q 116.404778 39.925194 2017-05-02 12:10:13
4 D3410D9N7C00184Q 116.446373 39.920338 2017-05-02 12:24:58
5 D3410D9N7C00184Q 116.477913 39.921471 2017-05-02 13:09:06
6 D3410D9N7C00184Q 116.477859 39.924759 2017-05-02 13:11:23
7 D3410D9N7C00184Q 116.519775 39.927376 2017-05-02 13:25:06
8 D3410D9N7C00184Q 116.517937 39.915039 2017-05-02 13:25:21
9 D3410D9N7C00184Q 116.518135 39.914917 2017-05-02 15:30:27
洗掉一行之后,重新設定了索引但是這時再想輸出csv_data[0]時 出錯了
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2525, in get_loc
return self._engine.get_loc(key)
File "pandas\_libs\index.pyx", line 117, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 139, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1265, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1273, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 0
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "E:/FirstPython/test02.py", line 20, in <module>
print(csv_data[0])
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2139, in __getitem__
return self._getitem_column(key)
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2146, in _getitem_column
return self._get_item_cache(key)
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py", line 1842, in _get_item_cache
values = self._data.get(item)
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\internals.py", line 3843, in get
loc = self.items.get_loc(item)
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2527, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas\_libs\index.pyx", line 117, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 139, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1265, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1273, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 0
uj5u.com熱心網友回復:
是不是沒有加行索引的原因,初學DataFrame,發現不加索引好像沒法對其進行操作,不知道樓主找到解決方法了么uj5u.com熱心網友回復:
csv_data.drop([0], inplace=True)這個已經把csv_data[0]去掉了,當然輸出就出輸了
uj5u.com熱心網友回復:
樓主你好,1、csv_data[0]表示列名為0的列,而不是行。你的列名沒有0的,自然會報錯,key error
你要輸出第一行,可以用print(csv_data.iloc[0]),這個肯定沒問題。如果第一列的index是0,也可以用csv_data.loc[0]
2、對于index,如果你沒有自行設定,而且沒有用set_index去設定復索引,默認就是[0,1,2..],我覺得沒有必要用reset_index陳述句
3、你取前十行,應該用csv_data.iloc[0:10]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/60803.html
