简体   繁体   English

如何创建词典列表

[英]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. 我想计算按年份分组的文档中单词出现频率的数据,然后将数据放入pandas数据框中。

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. 但是.append()导致错误消息,指出.append()不适用于字典类型。

wordtable needs to be a list of dictionaries. wordtable表必须是词典列表。 Then use pd.DataFrame.from_records 然后使用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. 正如前面提到的那样,append()是列表方法,而不是dict方法。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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