Writing multiple lists to csv Python

前端 未结 2 393
故里飘歌
故里飘歌 2021-01-23 20:27

Im trying to write a function that writes multiple lists to a singular csv file and i am able to get the column titles to write, but not any of the data. My data is in lists tha

2条回答
  •  旧时难觅i
    2021-01-23 21:10

    Try this to store the data in csv file with multiple list of different length. Here I tried to give header same as A and B as list name.

    import pandas as pd
    
    A = ['Apple', 'Dog']
    
    B = ['Cat', 'OWL', 'PEACOCK']
    
    dict_1 = {'A': A, 'B': B}
    
    df = pd.DataFrame.from_dict(dict_1, orient='index')
    
    dt = df.transpose()
    
    dt.to_csv('myfile.csv', index=False, header=True,  encoding='utf-8')
    

提交回复
热议问题