grok-3-preview-02-24· xAI
LMArena Human Preference 140K
Write a Python function to check for missing values in a Pandas DataFrame and report the…
- User
Write a Python function to check for missing values in a Pandas DataFrame and report the columns with null counts.
- Assistant
Here's a Python function that checks for missing values in a Pandas DataFrame and provides a detailed report of null counts per column:
import pandas as pd import numpy as np def check_missing_values(df, verbose=True): """ Check for missing values in a Pandas DataFrame and generate a report. Parameters: ----------- df : pandas.DataFrame The input DataFrame to check for missing values verbose : bool, default True If True, prints a detailed report Returns: -------- dict A dictionary containing: - total_missing: Total number of missing values in the DataFrame - missing_by_column: Dictionary of columns with their missing value counts - missing_percentage: Dictionary of columns with their missing value percentages """ # Calculate total missing values total_missing = df.isnull().sum().sum() # Get missing value counts per column missing_by_column = df.isnull().sum() # Calculate percentage of missing values per column total_rows = len(df) missing_percentage = (missing_by_column / total_rows * 100).round(2) # Create results dictionary results = { 'total_missing': total_missing, 'missing_by_column': missing_by_column.to_dict(), 'missing_percentage': missing_percentage.to_dict() } if verbose: print("\n=== Missing Values Report ===") print(f"Total number of rows: {total_rows}") print(f"Total missing values: {total_missing}") print(f"Percentage of total data missing: {(total_missing / (total_rows * len(df.columns)) * 100):.2f}%") # Create a summary DataFrame for display summary_df = pd.DataFrame({ 'Missing Values': missing_by_column, 'Percentage Missing': missing_percentage }) # Filter to show only columns with missing values summary_df = summary_df[summary_df['Missing Values'] > 0] if len(summary_df) > 0: print("\nColumns with missing values:") print("-" * 50) print(summary_df.sort_values(by='Missing Values', ascending=False)) else: print("\nNo missing values found in the DataFrame!") return results # Example usage if __name__ == "__main__": # Create sample DataFrame with missing values data = { 'A': [1, 2, np.nan, 4, 5], 'B': [1, np.nan, np.nan, 4, 5], 'C': [1, 2, 3, 4, 5], 'D': [np.nan, np.nan, np.nan, np.nan, np.nan] } df = pd.DataFrame(data) # Run the function results = check_missing_values(df) # Access the results dictionary if needed print("\nResults dictionary contents:") print(results)This function provides the following features:
-
Comprehensive Analysis:
- Counts total missing values in the DataFrame
- Identifies missing values per column
- Calculates percentage of missing values per column
-
Detailed Reporting (when verbose=True):
- Prints a formatted report showing:
- Total number of rows
- Total missing values
- Overall percentage of missing data
- Detailed breakdown of columns with missing values
- Percentage of missing values per column
- Prints a formatted report showing:
-
Return Value:
- Returns a dictionary containing all the analysis results for programmatic use
-
Error Handling:
- Works with any valid Pandas DataFrame
- Handles cases where there are no missing values
Example output for the sample DataFrame would look like this:
=== Missing Values Report === Total number of rows: 5 Total missing values: 10 Percentage of total data missing: 50.00% Columns with missing values: -------------------------------------------------- Missing Values Percentage Missing D 5 100.00 B 2 40.00 A 1 20.00 Results dictionary contents: { 'total_missing': 10, 'missing_by_column': {'A': 1, 'B': 2, 'C': 0, 'D': 5}, 'missing_percentage': {'A': 20.0, 'B': 40.0, 'C': 0.0, 'D': 100.0} }To use this function with your own DataFrame, simply:
# Load your DataFrame df = pd.read_csv('your_data.csv') # Run the analysis results = check_missing_values(df) # If you don't want the printed report, use verbose=False results = check_missing_values(df, verbose=False) # Access specific results total_missing = results['total_missing'] missing_by_col = results['missing_by_column'] missing_percent = results['missing_percentage']This function is particularly useful for:
- Data quality assessment
- Data cleaning preparation
- Exploratory data analysis
- Identifying columns that might need imputation or removal
You can enhance this function further by adding features like:
- Custom threshold for highlighting columns with significant missing values
- Visualization of missing value patterns
- Recommendations for handling missing values
- Export options for the report
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.