简体   繁体   English

计数熊猫并将输出分配给变量

[英]Counting in pandas and assigning output to a variable

I'm fairly new to python and pandas so forgive me if this is a somewhat basic question. 我是python和pandas的新手,如果这是一个基本问题,请原谅我。 I am reading in some data from a csv file, I want to do a tally from column 'gender' of 'M', 'F' and NaN. 我正在从csv文件中读取一些数据,我想从“ M”,“ F”和NaN的“性别”列中进行计数。 The code below outputs this: 下面的代码输出:

    import pandas as pd
    import numpy as np

    df = pd.read_csv("....csv")
    count = pd.value_counts(df['gender'],dropna=False)

This outputs: 输出:

    M      22
    F       3
    NaN     1

However, I don't want to just see these as a tally, I want the values to be assigned to variables. 但是,我不想仅仅将它们视为一个计数,而是希望将值分配给变量。 Ie have 即有

    male = pd.value_counts(df['gender'],'M',dropna=False)

or something similar, giving male = 22 (and the same for female and Nan), however I can't find an obvious way to do this using pandas. 或类似的方法,给雄性= 22(雌性和Nan相同),但是我找不到使用熊猫的明显方法。 Any advice? 有什么建议吗? Many thanks in advance! 提前谢谢了!

In this example we take the count of the gender series filtered by == "male" 在此示例中,我们以== "male"过滤的gender系列的计数

import pandas as pd
import random
df = pd.DataFrame({'gender': [random.choice(['male', 'female']) for x in range(100)]})
count_men = df[df["gender"] == "male"].count()
count_men

And if you just want the integer you can take it as the zeroth value: 如果只需要整数,则可以将其作为零值:

count_men = df[df["gender"] == "male"].count()[0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM