How to deal with missing values in a dataset using Python?
In the first example, we are going to fill missing values with the mean of the column in a dataset using Python. Here is the code:
Explanation: 1. We import the pandas library as pd to work with dataframes. 2. We create a sample dataset with missing values in columns A and B. 3. We use the fillna() function to fill missing values with the mean of each column. 4. The inplace=True parameter updates the original DataFrame with the filled values. 5. We print the updated DataFrame to see the result. In the second example, we are going to drop rows with missing values in a dataset using Python. Here is the code:
Explanation: 1. We import the pandas library as pd to work with dataframes. 2. We create a sample dataset with missing values in columns A and B. 3. We use the dropna() function to drop rows with any missing values. 4. The inplace=True parameter updates the original DataFrame by removing rows with missing values. 5. We print the updated DataFrame to see the result.
import pandas as pd # Create a sample dataset with missing values data = {'A': [1, 2, None, 4, 5], 'B': [5, 6, 7, None, 9]} df = pd.DataFrame(data) # Fill missing values with the mean of the column df.fillna(df.mean(), inplace=True) print(df)
Explanation: 1. We import the pandas library as pd to work with dataframes. 2. We create a sample dataset with missing values in columns A and B. 3. We use the fillna() function to fill missing values with the mean of each column. 4. The inplace=True parameter updates the original DataFrame with the filled values. 5. We print the updated DataFrame to see the result. In the second example, we are going to drop rows with missing values in a dataset using Python. Here is the code:
import pandas as pd # Create a sample dataset with missing values data = {'A': [1, 2, None, 4, 5], 'B': [5, 6, 7, None, 9]} df = pd.DataFrame(data) # Drop rows with missing values df.dropna(inplace=True) print(df)
Explanation: 1. We import the pandas library as pd to work with dataframes. 2. We create a sample dataset with missing values in columns A and B. 3. We use the dropna() function to drop rows with any missing values. 4. The inplace=True parameter updates the original DataFrame by removing rows with missing values. 5. We print the updated DataFrame to see the result.
Comments
Post a Comment