简体   繁体   English

计算熊猫中的列值

[英]counting column values in pandas

dictionary = {'Year': [1985, 1985, 1986, 1986, 1987, 1987, 1987]}
pdf = pd.DataFrame(dictionary)

    Year
0   1985
1   1985
2   1986
3   1986
4   1987
5   1987
6   1987

I have a dataframe called pdf I need to form a new data frame in the following format:我有一个名为pdf new data frame ,我需要按以下格式形成一个new data frame

Year   count
1985     2
1986     2 
1987     3

How can do this efficiently in pandas?如何在熊猫中有效地做到这一点?

.value_counts

pdf['Year'].value_counts()

Here is the answer:这是答案:

dictionary = {'Year': [1985, 1985, 1986, 1986, 1987, 1987, 1987]}
pdf = pd.DataFrame(dictionary)
gb = pdf.groupby('Year')['Year'].count()

Counter is a counter tool provided to support convenient and rapid tallies of dictionaries and other hashable objects. Counter是一个计数器工具,用于支持方便快捷地对字典和其他可散列对象进行计数。

from collections import Counter

df = pd.DataFrame(Counter(pd.DataFrame(dictionary).Year).items(), 
                  columns=['Year', 'Count'])

>>> print df
print(df)
   Year  Count
0  1985      2
1  1986      2
2  1987      3

%timeit pd.DataFrame(dictionary).groupby('Year')['Year'].count()
1000 loops, best of 3: 777 µs per loop

%timeit pd.DataFrame(Counter(pd.DataFrame(dictionary).Year).items(), columns=['Year', 'Count'])
1000 loops, best of 3: 672 µs per loop

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

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