ValueError: 没有名为node2的轴用于对象类型<class 'pandas.core.frame.DataFrame'>。[英] ValueError: No axis named node2 for object type <class &#39;pandas.core.frame.DataFrame&#39;>

本文是小编为大家收集整理的关于ValueError: 没有名为node2的轴用于对象类型<class 'pandas.core.frame.DataFrame'>。的处理方法,想解了ValueError: 没有名为node2的轴用于对象类型<class 'pandas.core.frame.DataFrame'>。的问题怎么解决?ValueError: 没有名为node2的轴用于对象类型<class 'pandas.core.frame.DataFrame'>。问题的解决办法?那么可以参考本文帮助大家快速定位并解决问题。

问题描述

import pandas as pd
import numpy as np
from datetime import datetime

data = {'date': ['1998-03-01 00:00:01', '2001-04-01 00:00:01','1998-06-01 00:00:01','2001-08-01 00:00:01','2001-05-03 00:00:01','1994-03-01 00:00:01'], 
        'node1': [1, 1, 2,2,3,2],
     'node2': [8,316,26,35,44,56],
     'weight': [1,1,1,1,1,1], }

df2 = pd.DataFrame(data, columns = ['date', 'node1','node2','weight'])


df2['date'] = pd.to_datetime(df2['date'])

l1 = [1990,1991,1992,1993,1994,1995,1996,1997,1998]
l2 = [1999,2000,2001]
ndf = df2[df2['date'].dt.year.isin(l1+l2)]

mask = ndf.groupby('node1','node2').apply(lambda x : (x['date'].dt.year.isin(l1)).any())
mask2 = ndf.groupby('node1','node2').apply(lambda x : (x['date'].dt.year.isin(l2)).any())

我遇到的错误 -

Traceback (most recent call last):
  File "datanew.py", line 32, in <module>
    mask = ndf.groupby('node1','node2').apply(lambda x : (x['date'].dt.year.isin(l1)).any())
  File "C:\Python27\lib\site-packages\pandas\core\generic.py", line 5159, in groupby
    axis = self._get_axis_number(axis)
  File "C:\Python27\lib\site-packages\pandas\core\generic.py", line 357, in _get_axis_number
    .format(axis, type(self)))
ValueError: No axis named node2 for object type <class 'pandas.core.frame.DataFrame'>

我在数据中定义了列'node2',但错误仍然在说没有node2的轴.似乎是什么问题?

推荐答案

您需要[]在groupby中:

.groupby(['node1','node2'])

mask = ndf.groupby(['node1','node2']).apply(lambda x : (x['date'].dt.year.isin(l1)).any())
print (mask)
node1  node2
1      8         True
       316      False
2      26        True
       35       False
       56        True
3      44       False
dtype: bool

mask2 = ndf.groupby(['node1','node2']).apply(lambda x : (x['date'].dt.year.isin(l2)).any())
print (mask2)
node1  node2
1      8        False
       316       True
2      26       False
       35        True
       56       False
3      44        True
dtype: bool

本文地址:https://www.itbaoku.cn/post/1727885.html