I've created individual groups of my data using the following statements:
df = pd.read_csv(file_path)
grouped = df.groupby(df.some_parameter)
What I would then like to do (in psuedo-code is):
for name, group in grouped:
'Some Text' + name = group
write to csv
The end result being a separate .csv file of each chunk of the original dataset.
This answer was very helpful to me - thanks @mkln.
I just wanted to add something specific to my own use case, which relates to the original point about file-naming ('Some Text' + name = group).
You could add the name and additional text, for example current date, to each csv filename, so I will create a function to return the current date and then use this for the filename.
Therefore:
from datetime import datetime
def cur_date():
return datetime.now().strftime("%Y-%m-%d")
for name, group in grouped:
group.to_csv('{}_{}.csv'.format(name, cur_date()))
You were almost there
for name, group in grouped:
group.to_csv(path_to_disk)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With