ValueError:对象类型<class'没有名为userID的轴pandas.core.frame.dataframe'=“”

时间:2018-03-07 10:30:16

标签: python pandas

=“”

我想计算索引userID的元素。我的数据框

       gender  adID  rating
userID                     
1           m   100      50
1           m   101     100
1           m   102       0
2           f   100      50
2           f   101     100
3           m   102      62
3           m   107      28
3           m   101      36
2           f   102      74
2           f   107     100
4           m   101      62
4           m   102      28
5           f   109      50
5           f   110     100
6           m   103      50
6           m   104     100
6           m   105       0

我试过

df.count('userID')

但得到了输出

  File "/home/mm/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py", line 5630, in count
    axis = self._get_axis_number(axis)
  File "/home/mm/anaconda3/lib/python3.6/site-packages/pandas/core/generic.py", line 357, in _get_axis_number
    .format(axis, type(self)))
ValueError: No axis named adID for object type <class 'pandas.core.frame.DataFrame'>

如何解决这个问题?索引操作是否遵循与列相同的原则?

1 个答案:

答案 0 :(得分:2)

你必须使用df.index,但是没有实现一些pandas功能,所以可以使用to_seriesSeries contructor:

a = df.index.value_counts()
print (a)
2    4
6    3
3    3
1    3
5    2
4    2
Name: userID, dtype: int64

b = len(df.index)
print (b)
17

c = df.index.to_series().mode()
print (c)
0    2
dtype: int64
相关问题