pandas groupby 熊猫中的错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19696047/
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-09-13 21:17:49  来源:igfitidea点击:

Error in groupby pandas

pythonnumpypandasscipy

提问by user1345283

I am running a script in windows and I get the following error:

我在 Windows 中运行脚本,但出现以下错误:

  Traceback (most recent call last):    
  File "C:\Users\esalazar\Desktop\datos\stat_cea_2011\emas\amealco\promedios-emas.py", line 64, in <module>
    g=index.groupby(level=0)    
    AttributeError: 'NoneType' object has no attribute 'groupby'

I have installed pandas, scipy and numpy. I need to install some other library? How do I fix this error?

我已经安装了 pandas、scipy 和 numpy。我需要安装其他一些库吗?我该如何解决这个错误?

This is part of my code:

这是我的代码的一部分:

data = pd.read_csv('C:/Users/esalazar/Desktop/datos/stat_cea_2011/emas/amealco/enero.csv',skiprows=1,names=['Fecha','Hora','C','D','E','Temperatura','TempRocio','DirViento','I','MagViento','K','Humedad','Presion','N','PreciAcu','P','Q','R','S'],header=0)
Uviento=[]
Vviento=[]

for i in range(0,len(data['MagViento'])):
    Uviento.append((data['MagViento'][i]*sin((data['DirViento'][i]+180)*(pi/180.0))))
    Vviento.append((data['MagViento'][i]*cos((data['DirViento'][i]+180)*(pi/180.0))))

data['PromeU']=Uviento
data['PromeV']=Vviento

data
<class 'pandas.core.frame.DataFrame'> 
Int64Index: 4463 entries, 0 to 4462
Data columns (total 19 columns):
Fecha          4463  non-null values
Hora           4463  non-null values
C              4463  non-null values
D              4463  non-null values
E              4463  non-null values
Temperatura    4463  non-null values
TempRocio      4463  non-null values
DirViento      4463  non-null values
I              4463  non-null values
MagViento      4463  non-null values
K              4463  non-null values
Humedad        4463  non-null values
Presion        4463  non-null values
N              4463  non-null values
PreciAcu       4463  non-null values
P              4463  non-null values
Q              4463  non-null values
R              4463  non-null values
S              4463  non-null values
dtypes: float64(8), int64(4), object(7)

df = data.set_index(['Fecha','Hora'],inplace=True)
df

i get this error

我收到这个错误

TypeError: NoneType object has no atribute '--getitem--'

therefore,

所以,

grouped = df.groupby(level=0)

is error too.

也是错误。

回答by sashkello

data.set_index(['Fecha','Hora'], inplace=True)modifies your DataFrame in place (see docs); this is what inplace=Truespecifies. That is, it doesn't create a new object but rather modifies datadirectly. You can do either

data.set_index(['Fecha','Hora'], inplace=True)就地修改您的 DataFrame(参见文档);这就是inplace=True指定的内容。也就是说,它不会创建新对象,而是data直接修改。你可以这样做

df = data.set_index(['Fecha','Hora'])
grouped = df.groupby(level=0)

or

或者

data.set_index(['Fecha','Hora'], inplace=True)
grouped = data.groupby(level=0)