就是我有一个表格,大致像下面这样子:
| | 0 | 1 | 2 | 3 | 4 | 5 |
| --- | --- | --- | --- | --- | --- | --- |
| 1 | A | A | B | B | A | B |
| 2 | B | A | B | A | A | A |
....
我需要统计每一行的 A,B 的个数或者 A,B 在这一行所占的百分比,有什么好的办法么
(比如上面的例子 1
有 3 个 A,3 个 B; 2
有 4 个 A,2 个 B )
perdata.apply(lambda x:x.count('~'),axis=0)
提示如下:
KeyError: ('Level ~ must be same as name (None)', 'occurred at index 0')
谢谢
1
ipwx 2018-03-23 16:03:47 +08:00 1
In [1]: import pandas as pd
In [2]: df = pd.DataFrame([list('AABBAB'), list('BABAAA')]) In [3]: df Out[3]: 0 1 2 3 4 5 0 A A B B A B 1 B A B A A A In [4]: df == 'A' Out[4]: 0 1 2 3 4 5 0 True True False False True False 1 False True False True True True In [5]: (df == 'A').sum(axis=1) Out[5]: 0 3 1 4 dtype: int64 |
3
congeec 2018-03-23 16:29:27 +08:00 via iPhone 1
虽然问题解决了,还是建议了解一下
from collections import Counter |
4
epicnoob 2018-03-23 16:34:47 +08:00 1
>>> pandas.value_counts(df.iloc[0])
B 3 A 3 Name: 0, dtype: int64 >>> pandas.value_counts(df.iloc[1]) A 4 B 2 Name: 1, dtype: int64 |