返回pandas.value_counts()中特定值的计数?

返回pandas.value_counts()中特定值的计数?,pandas,Pandas,假设运行pandas的数据帧['prod_code'].value_counts(),并将结果存储为'df'。操作输出: 125011 90300 762 72816 None 55512 7156 14892 75162 8825 我如何提取“无”的计数?我希望结果是55512 我试过了 >>> df.loc[df.index.isin(['None'])] >>> Series([], Name: prod_code, dt

假设运行pandas的数据帧['prod_code'].value_counts(),并将结果存储为'df'。操作输出:

125011  90300
   762  72816
  None  55512
  7156  14892 
 75162   8825
我如何提取“无”的计数?我希望结果是55512

我试过了

>>> df.loc[df.index.isin(['None'])]
>>> Series([], Name: prod_code, dtype: int64)
而且

>>> df.loc['None']
>>> KeyError: 'the label [None] is not in the [index]'

似乎您需要的是
None
,而不是字符串
'None'

df.loc[df.index.isin([None])]

df.loc[None]
编辑:

如果需要检查索引中的
NaN

print (s1.loc[np.nan])
#or
print (df[pd.isnull(df.index)])
样本:

s = pd.Series(['90300', '90300', '8825', '8825', '8825', None, np.nan])
s1 = s.value_counts(dropna=False)
print (s1)
8825     3
90300    2
NaN      2
dtype: int64

print (s1[pd.isnull(s1.index)])
NaN    2
dtype: int64


编辑1:

对于剥离空白:

s = pd.Series(['90300', '90300', '8825', '8825', '8825', 'None ', np.nan])
print (s)
0    90300
1    90300
2     8825
3     8825
4     8825
5    None 
6      NaN
dtype: object

s1 = s.value_counts()
print (s1)
8825     3
90300    2
None     1
dtype: int64

s1.index = s1.index.str.strip()
print (s1.loc['None'])
1
两件事

  • pd.Series([None]*2+[1]*3).value\u counts()
    自动删除
    None
  • pd.Series([None]*2+[1]*3).值\u计数(dropna=False)
    None
    转换为
    np.NaN
  • 这告诉我您的
    None
    是一个字符串。但是由于
    df.loc['None']
    不起作用,我怀疑您的字符串周围有空格

    尝试:

    或:


    尽管如此,我还是很好奇如何在索引中引用
    np.NaN

    s = pd.Series([1, 2], [0, np.nan])
    s.iloc[s.index.get_loc(np.nan)]
    
    2
    

    这不起作用>>>df.loc[df.index.isin([None])]系列([],名称:product_code,数据类型:int64)好的,返回什么
    print(df.index)
    ?它有31112行,所以它只打印第一个和最后10个值(不包括“None”)。我想我发现了问题。count_values()计数时将None计数为NaN,因此结果系列中这些字段的值为NaN,而不是None。并且值不会被删除?
    s = pd.Series(['90300', '90300', '8825', '8825', '8825', 'None ', np.nan])
    print (s)
    0    90300
    1    90300
    2     8825
    3     8825
    4     8825
    5    None 
    6      NaN
    dtype: object
    
    s1 = s.value_counts()
    print (s1)
    8825     3
    90300    2
    None     1
    dtype: int64
    
    s1.index = s1.index.str.strip()
    print (s1.loc['None'])
    1
    
    df.filter(regex='None', axis=0)
    
    df.index = df.index.to_series().str.strip().combine_first(df.index.to_series())
    df.loc['None']
    
    s = pd.Series([1, 2], [0, np.nan])
    s.iloc[s.index.get_loc(np.nan)]
    
    2