pandas python 如何计算数据帧中的记录数或行数 [英] pandas python how to count the number of records or rows in a dataframe
问题描述
显然是 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.
很酷的事情(或许多 w.r.t. pandas
之一)是,如果您有 NA
值,count 会考虑这一点.
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屋!