AttributeError: 'DataFrame' object has no attribute

前端 未结 4 2029
余生分开走
余生分开走 2021-02-03 22:49

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 panda         


        
相关标签:
4条回答
  • 2021-02-03 23:14

    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())
    
    0 讨论(0)
  • 2021-02-03 23:23

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

    0 讨论(0)
  • 2021-02-03 23:32

    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

    0 讨论(0)
  • 2021-02-03 23:37

    value_counts work only for series. It won't work for entire DataFrame. Try selecting only one column and using this attribute. For example:

    df['accepted'].value_counts()
    

    It also won't work if you have duplicate columns. This is because when you select a particular column, it will also represent the duplicate column and will return dataframe instead of series. At that time remove duplicate column by using

    df = df.loc[:,~df.columns.duplicated()]
    df['accepted'].value_counts()
    
    0 讨论(0)
提交回复
热议问题