Home > other >  [for] python dataframe index of small white problem
[for] python dataframe index of small white problem

Time:09-21

. # read. CSV format data the date and time of the two columns
Csv_data=https://bbs.csdn.net/topics/pd.read_csv (path, parse_dates={' date_time: [' date ', 'time']})
# use dataframe save data data
Csv_data=https://bbs.csdn.net/topics/pd.DataFrame (csv_data, columns=[' ID ', 'longitude, latitude,' date_time '])
ID number and time order # sort ()
Csv_data=https://bbs.csdn.net/topics/csv_data.sort_values (by=[' ID ', 'date_time'])
# reset index
Csv_data=https://bbs.csdn.net/topics/csv_data.reset_index (drop=True)
# choose before ten lines
Csv_data=https://bbs.csdn.net/topics/csv_data [0:10]
Csv_data. Drop ([0], inplace=True)
Csv_data=https://bbs.csdn.net/topics/csv_data.reset_index (drop=True)
Print (csv_data)

Csv_data data is as follows:

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

To delete a line, to set up the index but at this time again want to output csv_data [0] made a mistake when
Traceback (the most recent call last) :
File "C: \ ProgramData \ Anaconda3 \ lib \ site - packages/pandas/core/indexes \ base py", line 2525, in get_loc
Return the 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
The File "pandas \ _libs \ hashtable_class_helper pxi, line 1265, in pandas. _libs. Hashtable. PyObjectHashTable. Get_item
The 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 (the most recent call last) :
The File "E:/FirstPython test02. Py", line 20, in & lt; module>
Print (csv_data [0])
The File "C: \ ProgramData \ Anaconda3 \ lib \ site - packages/pandas/core/frame. Py", line 2139, in the __getitem__
Return the self. _getitem_column (key)
The File "C: \ ProgramData \ Anaconda3 \ lib \ site - packages/pandas/core/frame. Py", line 2146, in _getitem_column
Return the self. _get_item_cache (key)
The File "C: \ ProgramData \ Anaconda3 \ lib \ site - packages, pandas, core, generic. Py", line 1842, in _get_item_cache
Values=self. _data while forming. The get (item)
The File "C: \ ProgramData \ Anaconda3 \ lib \ site - packages, pandas, core, internals. Py", line 3843, in the 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 the 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
The File "pandas \ _libs \ hashtable_class_helper pxi, line 1265, in pandas. _libs. Hashtable. PyObjectHashTable. Get_item
The File "pandas \ _libs \ hashtable_class_helper pxi, line 1273, in pandas. _libs. Hashtable. PyObjectHashTable. Get_item
KeyError: 0

CodePudding user response:

Is not the cause of the row index, beginners DataFrame and found not indexed as if can not to manipulate it, don't know the landlord to find a solution

CodePudding user response:

Csv_data. Drop ([0], inplace=True)

This has put csv_data [0] removed, output, of course, is lost

CodePudding user response:

The original poster hello,
1, csv_data [0] said the column name of 0, not line, your column names is not 0, natural complains, key error
Do you want to output the first line, you can use the print (csv_data. Iloc [0]), the no problem for certain, if the first column of the index is 0, can also use csv_data. Loc [0]
2, regarding the index, if you don't have to set, and use set_index to set up the complex index, the default is [0..] , I think it is not necessary to use reset_index statement
3, you should take the first ten lines, with csv_data. Iloc [0:10]
  • Related