Pandas error: DataFrame object has no attribute 'loc'

I am new to pandas and am trying Pandas 10 minute tutorial with Pandas version 0.10.1. However, when I do the following, I get an error as shown below. print df working fine.

Why is .loc not working?

code

 import numpy as np import pandas as pd df = pd.DataFrame(np.random.randn(6,4), index=pd.date_range('20130101', periods=6), columns=['A','B','C','D']) df.loc[:,['A', 'B']] 

Error:

 AttributeError Traceback (most recent call last) <ipython-input-4-8513cb2c6dc7> in <module>() ----> 1 df.loc[:,['A', 'B']] C:\Python27\lib\site-packages\pandas\core\frame.pyc in __getattr__(self, name) 2044 return self[name] 2045 raise AttributeError("'%s' object has no attribute '%s'" % -> 2046 (type(self).__name__, name)) 2047 2048 def __setattr__(self, name, value): AttributeError: 'DataFrame' object has no attribute 'loc' 
+6
source share
2 answers

loc was introduced in 0.11 , so you need to update pandas to follow a 10 minute introduction .

+9
source

It seems strange to me that loc does not work on mine, because I have pandas 0.11, but here is something that will work for you, just use ix

 df.ix[:,['A','B']] 
0
source

Source: https://habr.com/ru/post/944225/


All Articles