简体   繁体   中英

How to create a list of dictionaries

I want to calculate data on the frequencies of words in documents grouped by year, and then place the data in a pandas dataframe.

My routine creates a dictionary for each row, containing words and frequencies as keys and values. I then want to loop through years, appending the dictionaries to each other to create a list of dictionaries which i convert into a dataframe.

Creating dataframes out of lists of dictionaries seems standard; and i can do it by manually creating the list.

I'd like to be able to do something like this:

wordtable = {'year':'1965','word1':20, 'word2': 250, 'word3': 125}
newrow={'year':'1966','word1':150, 'word4': 250, 'word2': 125}
wordtable.append(newrow)

df = pandas.DataFrame(wordtable, index=[0])
df.to_csv('testdata.csv')

But .append() leads to an error message stating .append() doesn't work with dictionary types.

wordtable needs to be a list of dictionaries. Then use pd.DataFrame.from_records

wordtable = [{'year':'1965','word1':20, 'word2': 250, 'word3': 125}]
newrow={'year':'1966','word1':150, 'word4': 250, 'word2': 125}
wordtable.append(newrow)

df = pd.DataFrame.from_records(wordtable)
df

在此处输入图片说明

As the previous poster mentioned, append() is a list method but not a dict method. This should work, though:

import pandas

word_data = []  # list type
word_counts_1 = {'year': '1965', 'word1':20, 'word2': 250, 'word3': 125}  # dict type
word_counts_2 = {'year':'1966','word1':150, 'word4': 250, 'word2': 125}  # dict type
word_data.append(word_counts_1)  # append 1st word count data to list, word_data
word_data.append(word_counts_2)  # append 2nd word count data to list, word_data
df = pandas.DataFrame(word_data)  # create data frame from word_data
df.to_csv('testdata.csv')  # write it out

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM