Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: count empty strings in a column

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

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.

like image 557
daiyue Avatar asked Apr 05 '18 16:04

daiyue


3 Answers

Several ways. Using numpy is usually more efficient.

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
like image 83
jpp Avatar answered Nov 10 '22 07:11

jpp


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
like image 22
Anton vBR Avatar answered Nov 10 '22 05:11

Anton vBR


You can use this to count empty values

df.isnull().sum()
like image 3
Nyla Khan Avatar answered Nov 10 '22 07:11

Nyla Khan