Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas error: 'DataFrame' object has no attribute 'loc'

I am new to pandas and is trying the Pandas 10 minute tutorial with pandas version 0.10.1. However when I do the following, I get the error as shown below. print df works 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'
like image 397
Nyxynyx Avatar asked May 03 '13 16:05

Nyxynyx


2 Answers

I came across this question when I was dealing with pyspark DataFrame. So, if you're also using pyspark DataFrame, you can convert it to pandas DataFrame using toPandas() method.

like image 87
fstang Avatar answered Sep 20 '22 19:09

fstang


loc was introduced in 0.11, so you'll need to upgrade your pandas to follow the 10minute introduction.

like image 40
Andy Hayden Avatar answered Sep 21 '22 19:09

Andy Hayden