Python AttributeError: 'DataFrame' 对象没有属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19392226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:39:11  来源:igfitidea点击:

AttributeError: 'DataFrame' object has no attribute

pythonpandasattributeerror

提问by user2884350

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

尝试在 ipython 中运行此文件时,我不断收到不同的属性错误......熊猫初学者所以也许我错过了一些东西

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?

有任何想法吗?

采纳答案by Andy Hayden

value_countsis a Seriesmethod rather than a DataFramemethod (and you are trying to use it on a DataFrame, clean). You need to perform this on a specific column:

value_countsSeries方法而不是DataFrame方法(并且您正在尝试在 DataFrame 上使用它clean)。您需要在特定列上执行此操作:

clean[column_name].value_counts()

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

value_counts在 DataFrame上执行通常没有意义,但我认为您可以通过展平基础值数组将其应用于每个条目:

pd.value_counts(df.values.flatten())

回答by szeitlin

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

要获取数据框中所有列的所有计数,只需 df.count()

回答by Barath M

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

value_counts 仅适用于系列。它不适用于整个 DataFrame。尝试仅选择一列并使用此属性。例如:

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