![](/img/trans.png)
[英]No axis named 1 for object type <class 'pandas.core.frame.DataFrame'>
[英]ValueError: No axis named userID for object type <class 'pandas.core.frame.DataFrame'
我想通過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'>
如何解決此問題?索引操作是否遵循與列操作相同的原則?
您必須使用df.index
,但是未實現某些pandas函數,因此可以使用to_series
或Series
構造函數:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.