0

I have a list of csv's with the same columns. Here is how the list looks like,

C:/Users/foo/bar/January01.csv
C:/Users/foo/bar/February01.csv
C:/Users/foo/bar/March01.csv
C:/Users/foo/bar/January02.csv
C:/Users/foo/bar/March02.csv

I want something like this, all csv's that start with January should copy the data into January dataframe and likewise for all months.

Can anyone help me on this?

0

1 Answer 1

2

You can first iterate trough your directory to find all months you have, then you pass again appending the dataframes and finally saves them:

import os
dir_name = #your dir
months = set()
for file in os.listdir(dir_name):
    months.add(file[:-2])

month_df = {month: pd.DataFrame() for month in months}
for file in os.listdir(dir_name):
    month_df[file[:-2]] = month[file[:-2]].append(pd.read_csv(os.join.path(dir_name, file)))

for month in month_df.keys():
    month_df[month].to_csv(month + '.csv', index=False)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.