Austen Novis
Austen Novis

Reputation: 444

How do you filter out rows with NaN in a panda's dataframe

I have a few entries in a panda dataframe that are NaN. How would I remove any row with a NaN?

Upvotes: 3

Views: 1783

Answers (1)

spencerlyon2
spencerlyon2

Reputation: 9686

Just use x.dropna():

In [1]: import pandas as pd

In [2]: import numpy as np

In [3]:

In [3]: df = pd.DataFrame(np.random.randn(5, 2))

In [4]: df.iloc[0, 1] = np.nan

In [5]: df.iloc[4, 0] = np.nan

In [6]: print(df)
          0         1
0  2.264727       NaN
1  0.229321  1.615272
2 -0.901608 -1.407787
3 -0.198323  0.521726
4       NaN  0.692340

In [7]: df2 = df.dropna()

In [8]: print(df2)
          0         1
1  0.229321  1.615272
2 -0.901608 -1.407787
3 -0.198323  0.521726

Upvotes: 6

Related Questions