kshnkvn
kshnkvn

Reputation: 956

How to save dataframe as separated csv files?

I have a list of dicts like this:

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]}
]

This is loaded into the dataframe without any problems:

dataframe = pandas.DataFrame(data)

There are a lot of such dictionaries, about 200,000 and a lot of categories, I want to save files sorted by categories into different csv files.

At first, I just tried to display all the elements for each category:

for item in range(len(dataframe['category'])):
    dataframe['category'][item]

But i have output like this:

'Games & Hobbies'
'Video Games'
'Business'
...

Just to save all this is not a problem, but there are a lot of records and I would like to separate them. Thanks in advance for your help.

Upvotes: 1

Views: 106

Answers (2)

BENY
BENY

Reputation: 323226

Using groupby

for x , y in df.groupby('category'):
    y.to_csv(x+'.csv')

Upvotes: 4

Umar.H
Umar.H

Reputation: 23099

something like :

for cat in df['category'].unique():
  df1 = df.loc[df['category'] == cat]
  df1.to_csv(f'{cat}.csv')

this will loop through your dataframe and save a csv per each unique category.

Upvotes: 2

Related Questions