Iterate through input file in Jupyter Notebook (Python)
Question:
I’m using a variable called input that is defined like this:
input = pd.read_csv('january_2017.csv')
then in the middle there is a lot of code
...
...
...
...
at the end i have a DataFrame called ‘df’ and i want it to export it as csv as in the line below:
df.to_csv('df_january_2017.csv')
The problem is that i have 32 files for 32 months. January_2017…December_2017, January_2018….December_2018,…January_2019…December_2019.
And i need to run the code for each of this files, is there a way that i can iterate through this files and it runs the code at the same time for each iteration.
Answers:
You can import os
and use os.listdir()
to get a list of all files in a directory.
import os
directory = "/directory"
for filename in in os.listdir(directory):
input = pd.read_csv(filename)
.
.
.
df.to_csv("df_"+filename)
see https://www.newbedev.com/python/howto/how-to-iterate-over-files-in-a-given-directory/ for more.
I’m using a variable called input that is defined like this:
input = pd.read_csv('january_2017.csv')
then in the middle there is a lot of code
...
...
...
...
at the end i have a DataFrame called ‘df’ and i want it to export it as csv as in the line below:
df.to_csv('df_january_2017.csv')
The problem is that i have 32 files for 32 months. January_2017…December_2017, January_2018….December_2018,…January_2019…December_2019.
And i need to run the code for each of this files, is there a way that i can iterate through this files and it runs the code at the same time for each iteration.
You can import os
and use os.listdir()
to get a list of all files in a directory.
import os
directory = "/directory"
for filename in in os.listdir(directory):
input = pd.read_csv(filename)
.
.
.
df.to_csv("df_"+filename)
see https://www.newbedev.com/python/howto/how-to-iterate-over-files-in-a-given-directory/ for more.