【发布时间】:2020-09-12 17:54:33
【问题描述】:
我使用下面的代码生成了一个 pandas 数据框,其中示例序列列类似于“0-0-0-1-0-0-2-0-0-0-0” 我将序列字符串拆分为不同的列
df = DataFrame(data, columns = ['id', 'sequence'])
print(df.sequence.str.split("-", expand=True))
0 1 2 3 4 5 6 7 8 9
0 0 0 0 1 0 0 2 0 0
1 0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0 0
3 0 1 0 0 2 0 0 0 0
4 0 0 0 3 0 0 0 0 0
5 0 0 0 0 0 4 0 0 0
如果我想统计每列出现0的次数,我该怎么做?
我根据Python Pandas Counting the Occurrences of a Specific value 尝试了类似这样的打印df[df.education == '9th'].count(),但我没有适当的列名,我不知道该怎么做。
你能帮忙吗?
【问题讨论】:
-
你的意思是
df.eq(0).sum()? -
请提供
data的最小示例。 - minimal reproducible example
标签: python pandas dataframe count pandas-groupby