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

查看:502
本文介绍了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天全站免登陆