-1

I'm trying to write the keys and values of a dictionary in a .csv file for the first time. I'd like two have two columns: A) keys and B) values.

I used NLTK to get the bigrams from a large text file:

frequency=nltk.FreqDist(bigrams)

The dictionary looks like this:

('word1', 'word2'),1

I'd like to write the most common, say 100 bigrams, into the .csv file. My code looks like this:

import pandas as pd
import csv
common=frequency.most_common(100)
pd.DataFrame(common, columns=['word', 'count']).to_csv("output.csv", index=False)

However, the results are not being written in two different columns (A and B), they are all written in the same column (column A)

How can I fix this? Thanks!

mxrdybxm
  • 13
  • 2
  • 2
    Might be helpful to provide sample data. – BigBen Feb 06 '23 at 18:45
  • 1
    *However, the results are not being written in two different columns (A and B), they are all written in the same column (column A)* No. The `to_csv` method does generate a valid csv file, here with two columns. But Excel is known to have an awful support for csv files... The most common problem is that, depending on the locale, Excel expects the delimiter to be different than the standard comma. What is declared as the field delimiter in your locale? – Serge Ballesta Feb 06 '23 at 18:56
  • @SergeBallesta this makes a lot of sense and compeltely fixed my problem! I chose a semicolon as the delimiter x.to_csv("output.csv", sep=";") and it works just fine. Thanks! :) – mxrdybxm Feb 07 '23 at 08:52

1 Answers1

-1

I tried this out with random data, maybe this will help -

common = {}
common[('panda', 'white')] = 2

x = pd.DataFrame(common.items(), columns=['word', 'count'])
x.head()

x.to_csv("output.csv", index=False)
  • Also might help to check out [this](https://stackoverflow.com/questions/18837262/convert-python-dict-into-a-dataframe) answer on converting dictionaries to dataframes! – Divij Kulshrestha Feb 06 '23 at 19:11