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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Import multiple CSV files into pandas Krayna 0 1,693 May-20-2021, 04:56 PM
Last Post: Krayna
  Python - Pandas writing blank files to file tdunphy 0 1,972 Jan-14-2021, 12:11 AM
Last Post: tdunphy
Question Pandas - Creating additional column in dataframe from another column Azureaus 2 2,915 Jan-11-2021, 09:53 PM
Last Post: Azureaus
  Pandas merge csv files karlito 2 3,138 Dec-16-2019, 10:59 AM
Last Post: karlito
  Need Help With Filtering Data For Excel Files Using Pandas eddywinch82 9 5,984 Aug-06-2019, 03:44 PM
Last Post: eddywinch82
  comparing two columns two different files in pandas nuncio 0 2,370 Jun-06-2018, 01:04 PM
Last Post: nuncio

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020