我有这样的字典列表:
sample = [
{'title': title, 'description': description, 'category': category, 'URLS': [1, 3, 4]},
{'title': title, 'description': description, 'category': category, 'URLS': [1, 3, 4, 5, 6, 7]},
{'title': title, 'description': description, 'category': category, 'URLS': [1]}
]
它将毫无问题地加载到数据框中:
dataframe = pandas.DataFrame(data)
有很多这样的词典,大约有200,000种,还有很多类别,我想将按类别排序的文件保存到不同的csv文件中。
首先,我只是尝试显示每个类别的所有元素:
for item in range(len(dataframe['category'])):
dataframe['category'][item]
但是我有这样的输出:
'Games & Hobbies'
'Video Games'
'Business'
...
仅保存所有这些都不是问题,但是有很多记录,我想将它们分开。在此先感谢您的帮助。
使用 groupby
for x , y in df.groupby('category'):
y.to_csv(x+'.csv')
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句