You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何不拼接字符串统计pandas DataFrame列表列元素的大写字母数量

可行实现方案

你可以直接嵌套遍历前n个走法字符串的每个字符做统计,完全不需要先拼接列表元素:

方案1:列表推导式(最简洁)

n = 10 # 可自定义要统计的前n个元素
count = [sum(c.isupper() for move in row[:n] for c in move) for row in df['moves']]

方案2:pandas apply写法(适合需要把统计结果存回DataFrame的场景)

n = 10
def count_upper(moves_list):
    return sum(c.isupper() for move in moves_list[:n] for c in move)

df['upper_count'] = df['moves'].apply(count_upper)
count = df['upper_count'].tolist()

两种方案运行后得到的count都是预期的[3, 1],逻辑上是直接对每个走法字符串的单个字符做大写判断,没有字符串拼接的额外开销。

内容的提问来源于stack exchange,提问作者PretendNotToSuck

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 04:24:03