Python Forum
Creating many csv files from Pandas - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Creating many csv files from Pandas (/thread-28616.html)



Creating many csv files from Pandas - EMA - Jul-26-2020

I have one csv file with thousands of rows ,and 3 columns( Dept, staffName , role).
What I need to do is :

1.create separate CSV file for every department , with all columns.
2.I need to follow a logic to name the csv file , as below :
The name of the csv file should start with Dept Name then (-) followed by staffName who is (Priority from high to low)
1. Manager , if no manager found within this Dept Then
2. look for office , if not found Then
3. look for admin .
Get this staffName( Example , Fiance-Alex.csv) where Alex is a manager for example .Also , in some Departments may be there are many managers , or many officers or many admins , just retrieve the first one found .

Basically , its like Vlookup in Excel , if manager is found get his name .

I was able to do this but there is lots of manual work , it should not be the case. I have 100s of lines (group by and csv lines )because I do it manually for every Dept , I am trying to use for loop , and If statement to Iterate over it but it's not working

Issues with my current code :
1. Some files for some reason they are empty .. while it should not be empty
2. Its very manual , I have to name the files myself , (Dept-staffname.csv)

Below are 2 lines of my code , I have 100s of lines 2 and 3 to name all csv files for every department .

Find_Managers_or_Officers= df2[(df2['Role'] =="Manager") | (df2['Role'] == "Officer")] # this one is to find managers or officers and in separate files , then I just copy the  name and save the file accordingly ...
folder_39 =df2.loc[df2['folder'] =="HR"]
folder_39.to_csv('HR-Alex.csv',index = False)
How this can be more automated ?

Many thanks