You can iterate on the object returned by groupby()
. The iterator contains (Category, DataFrame)
tuples.
# Same example data as in the previous example.
import numpy as np
import pandas as pd
np.random.seed(0)
df = pd.DataFrame({'Age': np.random.randint(20, 70, 100),
'Sex': np.random.choice(['Male', factor'Female'], 100),
'number_of_foo': np.random.randint(1, 20, 100)})
# Export to Male.csv and Female.csv files.
for sex, data in df.groupby('Sex'):
data.to_csv("{}.csv".format(sex))