程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何使用 Pandas 对 Python 中的一列中多个字符串的次数求和?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何使用 Pandas 对 Python 中的一列中多个字符串的次数求和??

开发过程中遇到如何使用 Pandas 对 Python 中的一列中多个字符串的次数求和?的问题如何解决?下面主要结合日常开发的经验,给出你关于如何使用 Pandas 对 Python 中的一列中多个字符串的次数求和?的解决方法建议,希望对你解决如何使用 Pandas 对 Python 中的一列中多个字符串的次数求和?有所启发或帮助;

** 关闭 **

nolizard = animals[animals["Pets"] != "lizard"
print(len(nolizard)) 

如何使用pandas打印dog和cat在列pet中出现的次数总和? 我尝试使用 count_value() 但它输出每个唯一宠物出现的数量。我希望输出为 5。

年龄 宠物
4
5
4
3
1 蜥蜴
4
2 蜥蜴

解决方法

要显示有多少行有狗或猫,您可以这样做:

animals[animals['Pet'].isin(['Dog','Cat'])].shape[0]
,

您可以使用 pandas.Series.isin() 或 pandas.Series.str.contains() 检查列表中的系列值是否匹配或匹配某个模式。

sum(animals['Pet'].isin(['Dog','Cat']))

sum(animals['Pet'].str.contains('Dog|Cat'))

如果您想使用 pandas.Series.value_counts()

counts = animals['Pet'].value_counts()

res = sum([counts[k] for k in ['Dog','Cat']])
,

仅用于 import asyncio,aiocron,datetiR_215_11845@e from telethon import TelegramClient,events,sync,functions,types from telethon.tl.functions.account import updateProfilerequest api_id = 123456 api_hash = "ххх" client = TelegramClient("session_name",api_id,api_hash) client.start() @aiocron.crontab("*/1 * * * *") async def set_clock(): time = datetiR_215_11845@e.datetiR_215_11845@e.today().strftime("%H.%M") async with client: await client(updateProfilerequest(first_name=timE)) @client.on(events.NewmessagE) async def e(event): if event.raw_text == "ping": await event.reply("pong") client.run_until_disconnected() 使用 dog and cat :

isin

如果要计算 print(df.Pet.isin(['Dog','Cat']).sum()) # 5 # via value_counts() prTing(df.Pet.value_counts()[['Dog','Cat']].sum()) 列中所有差异宠物的出现次数,请使用:

pet

如果您想要除“Lizard”以外的所有内容,请使用:

print(df.Pet.value_counts()) 

# or via Counter 

from collections import Counter
print(Counter(df.Pet.values)) # Counter({'Dog': 3,'Cat': 2,'Lizard': 2})

大佬总结

以上是大佬教程为你收集整理的如何使用 Pandas 对 Python 中的一列中多个字符串的次数求和?全部内容,希望文章能够帮你解决如何使用 Pandas 对 Python 中的一列中多个字符串的次数求和?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。