0

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])?

2 Answers 2

1

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

file = pd.DataFrame({'lists': d,'frequency':c})
file.to_csv('name')
Sign up to request clarification or add additional context in comments.

Comments

0

If you don't want to use pandas then you may simply change your for loop and write to the file as below:

import csv

with open('writeData.csv', mode='w') as file:
    writer = csv.writer(file)
    for i,l in enumerate(d):
        d_c = f"{d[i]}: {c[i]}"
        print(d_c)
        
        writer.writerow(d_c)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.