Snorrlaxxx
Snorrlaxxx

Reputation: 168

Pandas: Checking to see if an entry in a pandas dataframe column is empty

Suppose you have a dataframe with numbers that are of type float. How would you check if there is an entry that is empty?

Example df:

df = pd.DataFrame([(.21, ), (.01, .67), (.66, .03), (.21, .18)],
                  columns=['dogs', 'cats'])

No there cannot be NAN's.

Upvotes: 0

Views: 1772

Answers (4)

hkgyyf
hkgyyf

Reputation: 60

[index for index,row in df.iterrows() if any(np.isnan(row))]

by iterating all rows, this can show you the indices of rows with an empty

Upvotes: 0

Abhas Mehrotra
Abhas Mehrotra

Reputation: 119

When you run that command, the blank would itself become NaN. So I am not sure what your output would be? You want NaN to disappear? you can do this:-

df = pd.DataFrame([(.21, ), (.01, .67), (.66, .03), (.21, .18)],
                  columns=['dogs', 'cats']).fillna(" ")

output :- enter image description here

Upvotes: 0

NYC Coder
NYC Coder

Reputation: 7594

You can use this to replace NaN with whatever you want:

df.fillna(0, inplace=True)
df

   dogs  cats
0  0.21  0.00
1  0.01  0.67
2  0.66  0.03
3  0.21  0.18

Upvotes: 1

BENY
BENY

Reputation: 323226

Use isna with any

df.isna().any()
Out[103]: 
dogs    False
cats     True
dtype: bool

Or from info , you can know is the non-null is not equal to dataframe length

df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4 entries, 0 to 3
Data columns (total 2 columns):
dogs    4 non-null float64
cats    3 non-null float64
dtypes: float64(2)
memory usage: 192.0 bytes

Upvotes: 1

Related Questions