Need help writing a dataframe into a csv with the help of a loop

0 votes
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
Apr 17, 2018 in Python by aryya
• 7,460 points
3,379 views

1 answer to this question.

0 votes
Using the following logic you can arrive at the desired output

it was necessary to create an empty df, than populate it, then groupby meal and print to csv

It is absolutely necessary that you create an empty dataframe, then populate it and then group by the meals and then print it to a csv

[code]
main_df= pd.DataFrame()

    for name, df in data.items():
        main_df = pd.concat([main_df, df])  

    main_df_group = main_df.groupby('Meal')
    for name, group in main_df_group:
        mydf_group = group

        mydf_group.to_csv(meal+ ...)
[/code]
answered Apr 17, 2018 by anonymous

Related Questions In Python

0 votes
1 answer

Need help with the output of a code snippet.

If you write x.insert(2,3) and then print x ...READ MORE

answered Jun 23, 2020 in Python by Viraj Doshi
3,402 views
–1 vote
1 answer
0 votes
1 answer
0 votes
0 answers

Storing a list of arrays into a CSV file and retrieving it back in a different program

This is the code that I am ...READ MORE

Jun 7, 2018 in Python by aryya
• 7,460 points
2,383 views
0 votes
1 answer

Return a list inside a for loop while iterating over the elements of another list

The print() is getting called multiple times ...READ MORE

answered Sep 22, 2018 in Python by SDeb
• 13,300 points
5,036 views
0 votes
1 answer

Need help checking the validity of an image file in Python

I went through the Python documentation and ...READ MORE

answered Jan 18, 2019 in Python by Nymeria
• 3,560 points
2,290 views
0 votes
1 answer
0 votes
1 answer

Need help with making use of Pluck in Python

Hi, good question. Easy solution to be ...READ MORE

answered Jan 24, 2019 in Python by Nymeria
• 3,560 points
1,705 views
0 votes
1 answer

How can I convert a list of dictionaries from a CSV into a JSON object in Python?

You could try using the AST module. ...READ MORE

answered Apr 17, 2018 in Python by anonymous
3,459 views
0 votes
1 answer

Need help with searching a binary search tree

Instead of multiplying the number of nodes ...READ MORE

answered Apr 17, 2018 in Python by anonymous
874 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP