简体   繁体   English

如何计算 Pandas DataFrame?

[英]How do I count in a Pandas DataFrame?

import pandas as pd

# Read CSV data file:
df = pd.read_csv('~/nclab-data-read/titanic.csv')

# Port where most passengers embarked:
port = df['Embarked'].mode()[0]
**# Count these passengers:
n_port = df[['Name']].loc[df['Embarked'] == 1].count()[0]**

I believe I have something incorrect in the bottom row, but can't figure out what.我相信我在底行有一些不正确的地方,但不知道是什么。

count() returns the number of non-null values. count() 返回非空值的数量。 If applied to a DataFrame, it returns an array with 1 value per column (hence you need to take the index 0).如果应用于 DataFrame,它将返回一个每列有 1 个值的数组(因此您需要使用索引 0)。

When applied to a Series, you get the number directly.应用于系列时,您会直接获得号码。

n_port = df.loc[df['Embarked'] == 1, 'Name'].count()

Obviously, both lines will return the same result.显然,这两行都将返回相同的结果。

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

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