Pre-processing CSV Files

mail@pastecode.io avatar
unknown
python
2 months ago
1.1 kB
3
Indexable
Never
import pandas as pd

def load_csv_files(file1, file2, headers_file1=True, headers_file2=True):
    try:
        # Load the first CSV file based on headers flag
        df1 = pd.read_csv(file1, header=0 if headers_file1 else None)

        # Load the second CSV file based on headers flag
        df2 = pd.read_csv(file2, header=0 if headers_file2 else None)

        # Display the loaded data (optional)
        print("Data from", file1, ":\n", df1)
        print("\nData from", file2, ":\n", df2)

        return df1, df2

    except Exception as e:
        print("Error loading CSV files:", e)
        return None, None

# Specify the file names and header flags (adjust these according to your needs)
file1_name = "GPM.csv"
file2_name = "IMD.csv"

headers_file1 = False  # Change to False if file1 doesn't have headers
headers_file2 = True  # Change to False if file2 doesn't have headers

# Load CSV files based on header flags
data_frame1, data_frame2 = load_csv_files(file1_name, file2_name, headers_file1, headers_file2)
Leave a Comment