社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

来自csv熊猫的python计数行

StackBartender • 5 年前 • 1477 次点击  

colors.csv

id  name    rgb         is_trans
0   -1  Unknown 0033B2  f
1   0   Black   05131D  f
2   1   Blue    0055BF  t

您如何计算F&T的数量(如下所示)

colors_summary = colors.count('is_trans')
print(colors_summary)

寻找结果

is_trans    id  name    rgb
f   107 107 107
t   28  28  28
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/47825
 
1477 次点击  
文章 [ 2 ]  |  最新文章 5 年前
ahed87
Reply   •   1 楼
ahed87    6 年前

和stdlib的替代方案 csv & Counter 是的。

color_csv = """id  name    rgb         is_trans
0   -1  Unknown 0033B2  f
1   0   Black   05131D  f
2   1   Blue    0055BF  t"""

import csv
from collections import Counter
from io import StringIO

settings = dict(delimiter=' ', skipinitialspace=True)
creader = csv.reader(StringIO(color_csv), **settings)
headers = next(creader)
counter = Counter((row[-1] for row in creader))
print(counter)

Counter({'f': 2, 't': 1})
usher
Reply   •   2 楼
usher    6 年前

假设你有

color_df # dataframe object

你可以这样做:

result_df = color_df.groupby('is_trans').count()
print(result_df) # should give you what you ask for.