Dropping Pandas DataFrame Rows with Missing Values in a Specific Column
In data analysis, it's often necessary to deal with missing values. One common task is to remove rows with missing values in a particular column. For example, 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
To obtain a DataFrame with only rows where the "EPS" column is not null, we can use the following method:
df = df[df['EPS'].notna()]
This expression selects all the rows where the "EPS" column is not null and assigns the result to the new DataFrame df. The result is as follows:
STK_ID EPS cash STK_ID RPT_Date 600016 20111231 600016 4.3 NaN 601939 20111231 601939 2.5 NaN
By using the notna() method, we can effectively filter out missing values in the specified column and create a DataFrame that contains only the rows of interest.
The above is the detailed content of How to Remove Rows with Missing Values in a Specific Pandas DataFrame Column?. For more information, please follow other related articles on the PHP Chinese website!