Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Creating many csv files from Pandas
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

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Pandas merge csv files karlito 2 559 Dec-16-2019, 10:59 AM
Last Post: karlito
  Need Help With Filtering Data For Excel Files Using Pandas eddywinch82 9 1,193 Aug-06-2019, 03:44 PM
Last Post: eddywinch82
  comparing two columns two different files in pandas nuncio 0 880 Jun-06-2018, 01:04 PM
Last Post: nuncio

Forum Jump:

Users browsing this thread: 1 Guest(s)