Search code examples
pythonpandasexport-to-csvexport-to-excel

Create csv/excel file when using arrays


I am trying to export data like this

['sure', '2']: 169
['2', 'trash']: 1
['what', '2077']: 8
['us', '1']: 75
['1']: 61
['7']: 9
['7', 'years']: 9
['great']: 56
['2']: 39
['hi']: 69
['game', '5']: 1
['experience', '1010']: 1
['100%']: 10
['5']: 12
['5', 'hours']: 2
['game', '1010']: 7
['1010']: 40
['4']: 30
['90%']: 5
['exist', '7']: 1

... created by

my_list = sorted(df, reverse=True)

d = []
c = []


for i,l in enumerate(d):
    if c[i] == 1:
        d.pop(i)
        c.pop(i)

for i,l in enumerate(d):
    print(f"{d[i]}: {c[i]}")

I would like to export to excel file (or csv). Could you please tell me where I should include to_excel (to_csv) in order to print the lists (d[i]) and their frequencies (c[i])?


Solution

  • you can just do something like this after the data is generated

    file = pd.DataFrame({'lists': d,'frequency':c})
    file.to_csv('name')