AttributeError: 'DataFrame' object has no attribute

198,873

Solution 1

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())

Solution 2

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

Solution 3

value_counts() is now a DataFrame method since pandas 1.1.0

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.value_counts.html

Share:
198,873
Admin
Author by

Admin

Updated on August 19, 2021

Comments

  • Admin
    Admin almost 3 years

    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?

  • The Red Pea
    The Red Pea over 5 years
    df.count() produces a different result than df['col'].value_counts() aka series.value_counts()! But , your post is probably helpful for folks who want df.count()
  • Zeeshan Ahmad Khalil
    Zeeshan Ahmad Khalil over 4 years
    it is not working when I am getting column through iloc
  • peractio
    peractio over 3 years
    value_counts() is now a DataFrame method since pandas 1.1.0 -- posted another answer stackoverflow.com/a/65004307/9379924