groupby pandas中的错误

2024-04-20 11:41:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在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'

我已经安装了熊猫,小猫咪和小猫咪。我需要安装其他库吗?如何修复此错误?

这是我代码的一部分:

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

我知道这个错误

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

因此

grouped = df.groupby(level=0)

也是错误。


Tags: dfdataobject错误nullvaluesgroupbynon
1条回答
网友
1楼 · 发布于 2024-04-20 11:41:25

data.set_index(['Fecha','Hora'], inplace=True)就地修改数据帧(请参见docs);这是inplace=True指定的。也就是说,它不创建新对象,而是直接修改data。你也可以

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

或者

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

相关问题 更多 >