Remove empty rows from a CSV file

INTRODUCTION

“This Python script allows you to clean up a CSV file by removing all empty rows. You’ll provide the name of the input CSV file, and after processing, the script will save the cleaned data into a new CSV file. Additionally, you can choose to open the output file immediately after it is saved. This simple process helps ensure that your data remains tidy and ready for further analysis.”

PROGRAM

HERE IS THE FULL PROGRAM TO REMOVE EMPTY ROWS FROM CSV FILE

import pandas as pd
import os


input_file = input("Enter the name of the input CSV file (with .csv extension): ")
output_file = input("Enter the name for the output CSV file (with .csv extension): ")


df = pd.read_csv(input_file)
df_cleaned = df.dropna(how='all')  
df_cleaned.to_csv(output_file, index=False)

print(f"Empty rows removed. File saved as {output_file}.")


open_file = input("Do you want to open the output file? (yes/no): ").lower()

if open_file == 'yes':
    os.startfile(output_file)  
else:
    print("You chose not to open the file.")

EXPLANATION OF THE PROGRAM

1. Input File Name

 The program starts by asking the user to enter the name of the input CSV file (including the .csv extension). The input() function        takes the user’s input as a string and stores it in the variable input_file.

input_file = input("Enter the name of the input CSV file (with .csv extension): ")

2. Output File Name

 Next, the program prompts the user to enter a name for the output file, which will store the cleaned data.This file name is stored in the     output_file variable.
output_file = input("Enter the name for the output CSV file (with .csv extension): ")


3. Reading the CSV file

   The program uses the pandas library (imported as pd) to read the input CSV file. pd.read_csv(input_file) loads the CSV file into a DataFrame (a table-like structure) and stores it in the variable df.
df = pd.read_csv(input_file)

4. Removing Empty Rows

dropna(how='all') is a method that removes rows where all the values are missing (NaN values).This step cleans the data by discarding any completely empty rows.The cleaned data is stored in the df_cleaned DataFrame.
df_cleaned = df.dropna(how='all')

5. Saving the Cleaned Data

The cleaned DataFrame (df_cleaned) is saved as a new CSV file using the to_csv() method.output_file is the name of the file that the user provided earlier.index=False ensures that row numbers (indexes) are not included in the output file, which makes the file cleaner.
df_cleaned.to_csv(output_file, index=False)

6. Confirmation Message

After the cleaned file is saved, the program prints a confirmation message to let the user know that empty rows have been removed and the file has been saved.

print(f"Empty rows removed. File saved as {output_file}.")

7. Asking to Open the File

 The program asks the user if they want to open the output file by typing yes or no.lower() ensures that the input is converted to lowercase, so the answer is not case-sensitive.
open_file = input("Do you want to open the output file? (yes/no): ").lower()

8. Opening the Output File (if chosen)

If the user types “yes”, the program uses os.startfile(output_file) to open the output file. This command opens the file using the default program on the system for CSV files (such as Excel or Notepad).If the user types “no”.
if open_file == 'yes':
    os.startfile(output_file)
else:
    print("You chose not to open the file.")

OUTPUT

CONCLUSION

“In summary, this program efficiently cleans your CSV file by removing completely empty rows and saves the clean version into a new file. You get full control over specifying both the input and output file names, and you can choose to open the cleaned file right after it’s saved. This simple and user-friendly process helps ensure your data is ready for further use without any unnecessary empty rows.”

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top