Skip to content Skip to sidebar Skip to footer

What Is The Problem With The Pandas To Csv In My Code?

I am running this code for a project I am doing for fun to find patterns in Disneyland wait times: import pandas as pd import numpy as np import matplotlib.pyplot as plt df_pirates

Solution 1:

For the first error based on the code you don’t care about the specific date + time so do this:

with pd.ExcelWriter(f'{month.date()}.xlsx'):

This will convert the datetime object to a date object

Your second error is saying you are attempting to make a column that isn’t all unique an index which pandas won’t allow.

Maybe there are field you can combine or use another one?


Solution 2:

What got it fixed was changing the code from

for month, group in all_data.groupby(pd.Grouper(freq='M')):
    with pd.ExcelWriter(f'{month}.xlsx') as writer:
        for day, dfsub in group.groupby(pd.Grouper(freq='D')):
            dfsub.to_excel(writer, sheet_name='day')

to

for month, group in all_data.groupby(pd.Grouper(freq='M')):
    with pd.ExcelWriter(f'{month.strftime("%B %Y")}.xlsx') as writer:
        for day, dfsub in group.groupby(pd.Grouper(freq='D')):
            dfsub.to_excel(writer,sheet_name=str(day.date()))

with the suggestions that were made.


Post a Comment for "What Is The Problem With The Pandas To Csv In My Code?"