Home >Backend Development >Python Tutorial >How to Remove Rows with NaN Values from a Specific Pandas DataFrame Column?
When working with Pandas DataFrames, it's essential to handle missing data effectively. One common task is to remove rows where a particular column contains NaN values.
Consider the following DataFrame:
STK_ID EPS cash STK_ID RPT_Date 601166 20111231 601166 NaN NaN 600036 20111231 600036 NaN 12 600016 20111231 600016 4.3 NaN 601009 20111231 601009 NaN NaN 601939 20111231 601939 2.5 NaN 000001 20111231 000001 NaN NaN
The goal is to remove all rows where the 'EPS' column contains NaN values, resulting in the following DataFrame:
STK_ID EPS cash STK_ID RPT_Date 600016 20111231 600016 4.3 NaN 601939 20111231 601939 2.5 NaN
To accomplish this task, you can use the df.dropna() method, which drops rows where any value in the specified column is NaN. However, in this case, you only want to remove rows where the 'EPS' column contains NaN. To apply this specifically to the 'EPS' column, use the following code:
df = df[df['EPS'].notna()]
This code checks each row in the DataFrame if the value in the 'EPS' column is not NaN, and if it is not, it keeps the row. If it is NaN, it drops the row. The resulting DataFrame will contain only the rows where the 'EPS' column has non-NaN values.
The above is the detailed content of How to Remove Rows with NaN Values from a Specific Pandas DataFrame Column?. For more information, please follow other related articles on the PHP Chinese website!