pandas python如何计算数据框中的记录或行数 [英] pandas python how to count the number of records or rows in a dataframe

查看:749
本文介绍了 pandas python如何计算数据框中的记录或行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对Pandas来说显然是新手.我如何简单地计算数据框中的记录数.

Obviously new to Pandas. How can i simply count the number of records in a dataframe.

我原本以为这样的事情就这么简单,而且我似乎甚至无法在搜索中找到答案……可能是因为它太简单了.

I would have thought some thing as simple as this would do it and i can't seem to even find the answer in searches...probably because it is too simple.

cnt = df.count
print cnt

上面的代码实际上只是打印整个df

the above code actually just prints the whole df

推荐答案

关于您的问题...计算一个字段?我决定提出一个问题,但希望对您有帮助...

Regards to your question... counting one Field? I decided to make it a question, but I hope it helps...

说我有以下DataFrame

Say I have the following DataFrame

import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.normal(0, 1, (5, 2)), columns=["A", "B"])

您可以按以下方式计算一列

You could count a single column by

df.A.count()
#or
df['A'].count()

两者均为5.

一件很酷的事情(或很多pandas之一)是,如果您具有NA值,则应将计数考虑在内.

The cool thing (or one of many w.r.t. pandas) is that if you have NA values, count takes that into consideration.

所以,如果我这样做了

df['A'][1::2] = np.NAN
df.count()

结果将是

 A    3
 B    5

这篇关于 pandas python如何计算数据框中的记录或行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆