[Python] CSV file contents not being filtered correctly

Discussão em 'Python' iniciado por Stack, Maio 14, 2025.

  1. Stack

    Stack Membro Participativo

    Hi below is the code I am currently using

    I want to download the zipfile and filter the DATE_ADDED to only collect reports from 2025, however the csv file it exports out does not display any rows of data, leaving me to assume it is not interpreting the filter correctly. The CSV file outputted The format of the CSV file being taken in

    import requests
    import pandas as pd
    import zipfile
    import io

    print('Downloading started')
    url = 'https://www.accessdata.fda.gov/MAUDE/ftparea/patientproblemcode.zip'

    # Downloading the file by sending the request to the URL
    req = requests.get(url)

    # Specifying the file name directly
    filename = "Experiment.zip"

    # Writing the file to the local file system
    with open(filename, 'wb') as output_file:
    output_file.write(req.content)

    print('Downloading Completed')

    # Extracting the CSV file from the zip archive
    with zipfile.ZipFile(io.BytesIO(req.content)) as z:
    csv_filename = z.namelist()[0] # Assuming there's only one file in the zip
    with z.open(csv_filename) as f:
    df = pd.read_csv(f, delimiter='|')

    # Convert DATE_ADDED to datetime format
    df['DATE_ADDED'] = pd.to_datetime(df['DATE_ADDED'], format='%Y/%m/%d', errors='coerce')

    # Filter the DataFrame for rows after 2025/01/01
    filtered_df = df[df['DATE_ADDED'] > pd.Timestamp('2025/01/01')]

    # Create a CSV file with the filtered DataFrame values
    filtered_df.to_csv('Experiment3.csv', index=False)

    print("CSV file 'Experiment2.csv' created successfully.")


    I am expecting for only rows of reports from 2025 onwards to be displayed however it seems everything is being filtered off here

    Continue reading...

Compartilhe esta Página