Convert list of dictionaries to a pandas DataFrame

前端 未结 7 2422
清酒与你
清酒与你 2020-11-22 01:22

I have a list of dictionaries like this:

[{\'points\': 50, \'time\': \'5:00\', \'year\': 2010}, 
{\'points\': 25, \'time\': \'6:00\', \'month\': \"february\"         


        
7条回答
  •  天命终不由人
    2020-11-22 01:43

    Pyhton3: Most of the solutions listed previously work. However, there are instances when row_number of the dataframe is not required and the each row (record) has to be written individually.

    The following method is useful in that case.

    import csv
    
    my file= 'C:\Users\John\Desktop\export_dataframe.csv'
    
    records_to_save = data2 #used as in the thread. 
    
    
    colnames = list[records_to_save[0].keys()] 
    # remember colnames is a list of all keys. All values are written corresponding
    # to the keys and "None" is specified in case of missing value 
    
    with open(myfile, 'w', newline="",encoding="utf-8") as f:
        writer = csv.writer(f)
        writer.writerow(colnames)
        for d in records_to_save:
            writer.writerow([d.get(r, "None") for r in colnames])
    

提交回复
热议问题