Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'DataFrame' object has no attribute

I keep getting different attribute errors when trying to run this file in ipython...beginner with pandas so maybe I'm missing something

Code:

from pandas import Series, DataFrame  import pandas as pd  import json  nan=float('NaN') data = [] with open('file.json') as f: for line in f:     data.append(json.loads(line))  df = DataFrame(data, columns=['accepted', 'user', 'object', 'response']) clean = df.replace('NULL', nan) clean = clean.dropna()  print clean.value_counts()   AttributeError: 'DataFrame' object has no attribute 'value_counts' 

Any ideas?

like image 603
user2884350 Avatar asked Oct 15 '13 22:10

user2884350


People also ask

How do you solve a DataFrame object has no attribute?

If you try to call concat() on a DataFrame object, you will raise the AttributeError: 'DataFrame' object has no attribute 'concat'. You have to pass the columns to concatenate to pandas. concat() and define the axis to concatenate along.

Which is not an attribute of DataFrame object?

the reason of " 'DataFrame' object has no attribute 'Number'/'Close'/or any col name " is because you are looking at the col name and it seems to be "Number" but in reality it is " Number" or "Number " , that extra space is because in the excel sheet col name is written in that format.

What is a DataFrame object?

DataFrame is a 2-dimensional labeled data structure with columns of potentially different types. You can think of it like a spreadsheet or SQL table, or a dict of Series objects. It is generally the most commonly used pandas object.


2 Answers

value_counts is a Series method rather than a DataFrame method (and you are trying to use it on a DataFrame, clean). You need to perform this on a specific column:

clean[column_name].value_counts() 

It doesn't usually make sense to perform value_counts on a DataFrame, though I suppose you could apply it to every entry by flattening the underlying values array:

pd.value_counts(df.values.flatten()) 
like image 107
Andy Hayden Avatar answered Oct 04 '22 19:10

Andy Hayden


To get all the counts for all the columns in a dataframe, it's just df.count()

like image 26
szeitlin Avatar answered Oct 04 '22 19:10

szeitlin