AttributeError: 'DataFrame' 对象没有属性 [英] AttributeError: 'DataFrame' object has no attribute

查看:69
本文介绍了AttributeError: 'DataFrame' 对象没有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

代码:

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'

有什么想法吗?

推荐答案

value_counts 是一种 Series 方法而不是 DataFrame 方法(并且您正试图在 DataFrame 上使用它, clean).您需要在特定列上执行此操作:

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

在 DataFrame 上执行 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())

这篇关于AttributeError: 'DataFrame' 对象没有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆