python - 为什么 df.value_counts() 的计数总和与 df 的总行数不同?
问题描述
我有一个数据框day_1
,我想在其中计算每个唯一行的计数。我这样做了day_1.value_counts()
。很奇怪,day_1.shape[0]
与 不同np.sum(day_1.value_counts())
。
你能详细说明一下这个问题吗?
import pandas as pd
# Import dataset
path = r'https://raw.githubusercontent.com/leanhdung1994/BigData/main/2_days.csv'
trends = pd.read_csv(path, header = 0, low_memory = False)
# Subset to a specific day
day_1 = trends[trends.date == '2021-01-01']
# Remove unused columns
columns_to_drop = ['date', 'hour', 'id', 'year', 'month', 'sentence',
'offset', 'span', 'value', 'container']
day_1 = day_1.drop(columns = columns_to_drop)
print('The total number of rows is', day_1.shape[0])
print('The total number of rows of all groups is', np.sum(day_1.value_counts()))
解决方案
推荐阅读
- spring - 将 select 子句中使用的别名表达式用于条件查询的 order by 子句
- database - 在线托管的网站是否可以访问本地服务器上的数据库?
- api - 来自 axios.post 的未定义响应
- mysql - Mysql - 添加当前时间戳字段而不影响现有记录
- python - 使用自定义标签解析文件
- angular - 错误:请将您的依赖项升级到 core-js@3 的实际版本
- typescript - 如何在打字稿中定义和使用静态变量
- excel - 使用 Excel VBA 重命名多个文件
- android - 找不到提供程序错误的元数据
- c++ - 是否可以在源文件中更改 constexpr-ops-limit ?