更改

使用Pandas统计元素出现次数

添加382字节, 2023年2月11日 (六) 17:39
撤销51.142.170.252讨论)的版本8685
[[File:PythonPandasStat7.png]]
8.使用print((df == 21).sum())即可将DataFrame中所有值 为2 为1 的数据个数统计。 import pandas as pd import numpy as np df = pd.DataFrame({'第一列':['a',1,'1','one','two'],'第二列':['a','one',1,2,'one'],'第三列':['a',2,1,'one','b']}) print(df) print('数字1的总次数:\n',(df==1).sum()) print('单词one的出现次数:\n',(df=='one').sum()) print('字母a的出现总次数:\n',(df=='a').sum()) [[File:PythonPandasStat8.png]]
1,138
个编辑