繁体   English   中英

如何在 dataframe 的每一列中找到特定字符串的 value_count

[英]How to find the value_count of a specific string in each column of the dataframe

我想找到字符串'\N'出现在 dataframe df的每一列中的次数。

我试过这个:

for col in df.columns: 
   print(df[col].value_counts()['\N'])

并且系统返回错误,例如

unicode 错误 unicode 无法在 position 中解码 0-1

你知道怎么解决吗?

反斜杠 () 字符用于转义具有特殊含义的字符,例如换行符、反斜杠本身或引号字符( 参见 python 词法分析

假设这个df:

    a   b
0  \N   1
1  \N   4
2   K  \N

使用您的代码将产生:

for col in df.columns:    
    print(df[col].value_counts()['\N'])

  File "<ipython-input-83-64eb7c05f66f>", line 2
    print(df[col].value_counts()['\N'])
                                ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: malformed \N character escape

如果添加额外的反冲,您将获得:

for col in df.columns:    
          print(f"{col} has",df[col].value_counts()['\\N']," \\N in it")

a has 2  \N in it
b has 1  \N in it

如果您使用df.to_dict() ,您也可以清楚地看到这一点:

>>> df.to_dict()
Out[901]: {'a': {0: '\\N', 1: '\\N', 2: 'K'}, 'b': {0: '1', 1: '4', 2: '\\N'}}
                      ^         ^                                         ^

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM