I am trying to write a df to a csv from a loop, each line represents a df, but I am finding some difficulties once the headers are not equal for all dfs, some of them have values for all dates and others no.
I'm trying to write a data frame to csv using a loop. I'm having some difficulties doing the same once the headers of the dataframes start changing for example some of the values have dates and others dont.
I'm using a function similar to this one for writing :
[code]
def write_csv():
for name, df in data.items():
df.to_csv(meal+'mydf.csv', mode='a')
[/code]
and it creates a csv for each meal (lunch an dinner) each df is similar to this:
Name Meal 22-03-18 23-03-18 25-03-18
Peter Lunch 12 10 9
or:
Name Meal 22-03-18 23-03-18 25-03-18
Peter Dinner 12 10 9
I have previously tried to use pandas concatenate, but I simply cant find a way to implement this in the function.
My goal is to have the headers with all the dates independent of the dataframes appended to the csv.
Actual output:
Name Meal 22-03-18 23-03-18 25-03-18
Peter Lunch 12 10 9
Mathew Lunch 12 11 11 10 9
Ruth Lunch 9 9 8 9
Anna Lunch 10 12 11 13 10
output with headers:
Name Meal 22-03-18 23-03-18 25-03-18
Peter Lunch 12 10 9
Name Meal 21-03-18 22-03-18 23-03-18 24-03-18 25-03-18
Mathew Lunch 12 11 11 10 9
Name Meal 21-03-18 22-03-18 24-03-18 25-03-18
Ruth Lunch 9 9 8 9
Name Meal 21-03-18 22-03-18 23-03-18 24-03-18 25-03-18
Anna Lunch 10 12 11 13 10
Output desired:
Name Meal 21-03-18 22-03-18 23-03-18 24-03-18 25-03-18
Peter Lunch 12 10 9
Mathew Lunch 12 11 11 10
Ruth Lunch 9 9 8 9
Anna Lunch 10 12 11 13 10