Create .CSV | .XLSX Export For Each Unique Value in Field within Pandas Dataframe


#1
# Create Data Frame
df = pd.DataFrame(csv)

# Build List of All Analytes
df_analytes = df.groupby(["Analyte"], as_index=False).count()
analytes = df_analytes['Analyte'].tolist()

# Export Unique Spreadsheet For All Analytes
for idx, analyte in enumerate(analytes, 1):
    filter = 'Analyte=="' + analyte + '"'
    filename_xlsx = analyte.replace('"', '') + '.xlsx'
    filename_csv = analyte.replace('"', '') + '.csv'
    df_filtered = df.query(filter)
    df_filtered.to_excel(filename_xlsx)
    df_filtered.to_csv(filename_csv)