Home > other >  Jupyter notebook df KeyError: the column index
Jupyter notebook df KeyError: the column index

Time:12-21

Using jupyter notebook, want to choose dataframe column, want to choose the column index as the close of the data, for example, using df [' close '] or df close all error, small white said don't understand error information, under the bosses help to find a reason



 the from pandas_datareader import data 
Import a datetime
The import pandas
The from bokeh. Plotting import figure, show, output_file

Start=datetime. Datetime (2016,3,1)
End=datetime. Datetime (2016 filling)

Df=data. The DataReader (name="popular", data_source="yahoo", start=start and end=end)
Print (df)

The results for the High Low Open Close Volume Adj Close
The Date
The 2016-02-29 140865200 22.516653 24.557501 24.162500 24.215000 24.172501
The 2016-03-01 201628400 23.410894 25.192499 24.355000 24.412500 25.132500
The 2016-03-02 132678400 23.462128 25.222500 24.910000 25.127501 25.187500
The 2016-03-03 147822800 23.636778 25.427500 25.112499 25.145000 25.375000

 df. Loc [:, df [' close ']] 

Error:
 KeyError Traceback (the most recent call last) 
D: \ Anaconda3 \ lib \ site - packages/pandas/core/indexes \ base py in get_loc (self, key, method, how)
2897 try:
-> 2898 the return of the self. _engine. Get_loc (casted_key)
2899 the except KeyError as err:

Pandas \ _libs \ index. Pyx in pandas. _libs. Index. IndexEngine. Get_loc ()

Pandas \ _libs \ index. Pyx in pandas. _libs. Index. IndexEngine. Get_loc ()

Pandas \ _libs \ hashtable_class_helper. Pxi in pandas. _libs. Hashtable. PyObjectHashTable. Get_item ()

Pandas \ _libs \ hashtable_class_helper. Pxi in pandas. _libs. Hashtable. PyObjectHashTable. Get_item ()

KeyError: 'close'

The above exception was The direct cause of The following exception:

KeyError Traceback (the most recent call last)

- & gt; 1 df. Loc [:, df [' close ']]

D: \ Anaconda3 \ lib \ site - packages, pandas, core, frame, py in the __getitem__ (self key)
2904 if the self. The columns. Nlevels & gt; 1:
2905 the return of the self. _getitem_multilevel (key)
-> 2906 the indexer=self. Columns. Get_loc (key)
2907 if is_integer (indexer) :
2908 the indexer=[indexer]

D: \ Anaconda3 \ lib \ site - packages/pandas/core/indexes \ base py in get_loc (self, key, method, how)
2898 the return of the self. _engine. Get_loc (casted_key)
2899 the except KeyError as err:
-> 2900 the -raise KeyError (key) from err
2901
If how is not None: 2902

KeyError: 'close'


Change
 df. Loc [:, df, close] 

An error
 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
AttributeError Traceback (the most recent call last)

- & gt; 1 df. Loc [:, df. Close]

D: \ Anaconda3 \ lib \ site - packages, pandas, core, generic. Py in.__getattr__ (self, name)
5139 if self. _info_axis. _can_hold_identifiers_and_holds_name (name) :
5140 return self [name]
-> 5141 the return object. The.__getattribute__ (self, name)
5142
5143 def __setattr__ (self, name: STR, value) - & gt; None:

AttributeError: 'DataFrame' object has no attribute 'close'


To change, add a line in front of the statement:
 the features=list (df) columns) 
Print (features)

 [' High ', 'Low', 'Open' and 'Close' and 'Volume' and 'Adj Close'] 


In the column index:
 df. Loc [:, the features [then]] 

Results:
 
The Open Close
The Date
The 2016-02-29 24.215000 24.172501
The 2016-03-01 24.412500 25.132500
The 2016-03-02 25.127501 25.187500
The 2016-03-03 25.145000 25.375000




CodePudding user response:

Know, first letter should be capitalized, close,
  • Related