我试图找出某个值在一列中出现的次数。
我用data = pd.DataFrame.from_csv('data/dataset2.csv')
制作了数据帧
现在我想找出某个东西出现在列中的次数。怎么做到的?
我以为是在下面,我在“教育”栏中查找并计算发生的时间。
下面的代码显示我正在尝试查找出现9th
的次数,错误是我运行代码时遇到的
代码
missing2 = df.education.value_counts()['9th']
print(missing2)
错误
KeyError: '9th'
Tags:
使用
count
或sum
的两种方法您可以使用条件创建数据的} 或
subset
,然后使用^{len
:性能很有趣,最快的解决方案是比较numpy数组和
sum
:代码:
计算任何列中出现的
'?'
或任何符号的优雅方法是使用dataframe对象的内置函数isin
。假设我们已将“Automobile”dataset加载到
df
对象中。 我们不知道哪些列包含缺少的值('?'
符号),因此请执行以下操作:DataFrame.isin(values)
官方文件说:注意,
isin
接受iterable作为输入,因此我们需要将包含目标符号的列表传递给此函数。df.isin(['?'])
将返回一个布尔数据帧,如下所示。要计算每列中目标符号的出现次数,让我们通过指示
axis=0
来对上述数据帧的所有行进行sum
。 最终(截断)结果显示了我们的期望:相关问题 更多 >
编程相关推荐