pandas 熊猫:计算列中的空字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49677060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:25:32  来源:igfitidea点击:

Pandas: count empty strings in a column

pythonstringpandasdataframeseries

提问by daiyue

I tried to find the number of cells in a column that only contain empty string ''. The dflooks like:

我试图找到只包含空字符串的列中的单元格数''。的df样子:

currency
USD
EUR
ILS
HKD

The code is:

代码是:

df['currency'].str.contains(r'\s*')

but the code also recognizes cells with actual string values as containing empty strings.

但该代码还将具有实际字符串值的单元格识别为包含空字符串。

I am wondering how to fix this issue that it only detects cells that only contains empty strings.

我想知道如何解决这个问题,它只检测只包含空字符串的单元格。

回答by jpp

Several ways. Using numpyis usually more efficient.

几种方式。使用numpy通常更有效。

import pandas as pd, numpy as np

df = pd.DataFrame({'currency':['USD','','EUR','']})

(df['currency'].values == '').sum()           # 2

len(df[df['currency'] == ''])                 # 2

df.loc[df['currency'] == ''].count().iloc[0]  # 2

回答by Anton vBR

Couldn't find the dupe so posting an answer:

找不到骗子所以发布答案:

import pandas as pd
df = pd.DataFrame({'currency':['USD','','EUR','']})
c = (df['currency'] == '').sum()
print(c)

Returns:

返回:

2