如何使用value_counts()返回的值进行进一步的计算?

时间:2019-05-11 10:17:41

标签: python python-3.x pandas dataframe

我有一个名为y_ocsvm的列,在名为step1的df中填充了1和-1。

我使用:step1['y_ocsvm'].value_counts()来获取1和-1的计数,输出是:

step1['y_ocsvm'].value_counts()
Out[11]: 
 1    1622
-1     426
Name: y_ocsvm, dtype: int64

我想找到-1的数量与1的数量之比。我可以简单地执行426/1622,但是由于我必须将其用于许多数据帧,因此这些值肯定会有所不同,这将使得手动计算该值变得困难。

由于value_counts()仅适用于熊猫系列,我尝试这样做:

pd.Series([step1['y_ocsvm'] == -1]).value_counts()

但是出现以下错误:

pd.Series([step1['y_ocsvm'] == -1]).value_counts()
Traceback (most recent call last):

  File "<ipython-input-13-59f772263a54>", line 1, in <module>
    pd.Series([step1['y_ocsvm'] == -1]).value_counts()

  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\pandas\core\base.py", line 1303, in value_counts
    normalize=normalize, bins=bins, dropna=dropna)

  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\pandas\core\algorithms.py", line 705, in value_counts
    keys, counts = _value_counts_arraylike(values, dropna)

  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\pandas\core\algorithms.py", line 750, in _value_counts_arraylike
    keys, counts = f(values, dropna)

  File "pandas/_libs/hashtable_func_helper.pxi", line 348, in pandas._libs.hashtable.value_count_object

  File "pandas/_libs/hashtable_func_helper.pxi", line 359, in pandas._libs.hashtable.value_count_object

  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\pandas\core\generic.py", line 1816, in __hash__
    ' hashed'.format(self.__class__.__name__))

SystemError: <built-in method format of str object at 0x00000203B7063AC0> returned a result with an error set

我想知道如何使用熊猫来做到这一点?

2 个答案:

答案 0 :(得分:3)

这里不需要Series构造函数,因为step1['y_ocsvm'] == -1Series填充了布尔值:

out = (step1['y_ocsvm'] == -1).value_counts()

可能使用比率:

print (out[True] / out[False])

答案 1 :(得分:2)

您也可以

create table `tbltest` (
    `id` int(10) unsigned not null auto_increment,
    `name` varchar(50) null default null,
    `age` tinyint(3) unsigned null default null,
    `gender` varchar(6) not null default 'male',
    `status` bit(1) not null default b'0',
    primary key (`id`)
)
engine=innodb;


+--------+---------------------+------+-----+---------+----------------+
| Field  | Type                | Null | Key | Default | Extra          |
+--------+---------------------+------+-----+---------+----------------+
| id     | int(10) unsigned    | NO   | PRI | NULL    | auto_increment |
| name   | varchar(50)         | YES  |     | NULL    |                |
| age    | tinyint(3) unsigned | YES  |     | NULL    |                |
| gender | varchar(6)          | NO   |     | Male    |                |
| status | bit(1)              | NO   |     | b'0'    |                |
+--------+---------------------+------+-----+---------+----------------+


+----+----------+------+--------+--------+
| id | name     | age  | gender | status |
+----+----------+------+--------+--------+
|  1 | Rinku    |   23 | Male   | 1      |
|  2 | Ricky    |   21 | Male   |        |
|  3 | Samantha |   15 | Female | 1      |
+----+----------+------+--------+--------+