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.
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
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
You can use this to count empty values
df.isnull().sum()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With