【发布时间】:2019-05-03 21:50:40
【问题描述】:
我从大量数据中创建了一个默认字典,其中包含如下预览的列表值。 default_dictionary 值在默认字典中表示为列表。
default_dict = {('text2015', 'webnet'): [52384, 22276, 97376, 93696, 12672], ('datascience', 'bigdata', 'links'): [18720], ('biological', 'biomedics', 'datamining', 'datamodel', 'semantics'): [82465, 43424], ('links', 'scientometrics'): [23297, 73120]}
我有另一个 data_dictionary,它具有来自 default_dictionary 的单个列表值作为键。 data_dictionary 中的顺序是 (key_ID : [text_values], total, guser_ID)。数据字典的格式为:
data_dictionary = {52384: (['text2015', 'webnet'], 1444856137000, 335829830), 18720: (['datascience', 'bigdata', 'links'], 1444859841000, 17987803), 82465: (['biological', 'biomedics', 'datamining', 'datamodel', 'semantics'], 1444856, 335829830), 73120: (['links', 'scientometrics'], 144481000, 17987803), 22276: (['text2015', 'webnet'], 1674856137000, 615387550), 97376: (['text2015', 'webnet'], 1812856137000, 371559830), 43424: (['biological', 'biomedics', 'datamining', 'datamodel', 'semantics'], 5183856, 363549260), 23297: (['links', 'scientometrics'], 1614481000, 26253825)}
值列表中的第二个选项(总和)是我希望用来比较不同键的数字。这是一个总金额。我希望在 CSV 文件中首先显示总和最小的 key_ID,然后显示总和更大的 ID,依此类推,如下所示。一句话:
(key_ID(least sum); key_ID ; sum for (least sum) key_ID ; sum for other key _Id ; shared text)
> 52384 ; 22276 ; 1444856137000 ; 1674856137000 ; ['text2015', 'webnet']
> 52384 ; 97376 ; 1444856137000 ; 1812856137000 ; ['text2015', 'webnet']
> 18720 ; 18720 ; 1444859841000 ; 1444859841000 ; ['datascience','bigdata', 'links']
> 82465 ; 43424 ; 1444856 ; 5183856 ;['biological', 'biomedics', 'datamining', 'datamodel', 'semantics']
> 73120 ; 23297 ; 144481000 ; 1614481000 ; ['links', 'scientometrics']
到目前为止,我一直在尝试使用字典来构建值并使用 pandas 打印为 csv,但没有取得太大成功。任何想法都会有帮助。此代码为每个文本提供其自己的共享该文本的 key_ID 的单独 csv 文件。
for key, value in default_dict.items():
df = pd.DataFrame(value)
df.to_csv('graph' + '_'.join(key) + '.csv', index=False)
【问题讨论】:
标签: python pandas csv dictionary dataframe