Python Forum
Creating a new DataFrame from another DataFrame column
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating a new DataFrame from another DataFrame column
#1
Hi All,

I'm newbie to python. I've a task to automate wherein I have to create separate excel files based on the RTO code available in an excel sheet using the main data available in another excel sheet.

import pandas as pd
import xlrd

Rto_Main_Sheet = pd.read_excel("D:\Testing_MIS\Auto_Task\FINAL GLM SHEET.xlsx",sheet_name='Jeep tata mahindra')
Rto_Main_Sheet.head(5)
Output:
ID Combined - TA100 RTO Code Age Discount 0 1 FICH AN01 0 50 1 2 FIWR AN01 0 50 2 3 FIRN AN01 0 50 3 4 FICO AN01 0 50 4 5 JPWR AN01 0 70
Rto_Code = pd.read_excel("D:\Testing_MIS\Auto_Task\RTO_Code_AN01-HP46.xlsx",sheet_name='RTO_Code_AN01-HP46')
Rto_Code.head(5)
Output:
RTO 0 AN01 1 AN02 2 AP01 3 AP02 4 AP03
[/python]

Now, for each RTO Code available have to create a separate DataFrame and write it to excel file. The output should be as follows:-
Here, only the RTO Code has to be changed and rest all the data of the column would be as is from the Rto_Main_Sheet dataframe.

Output:
ID Combined - TA100 RTO Code Age Discount 0 1 FICH AN02 0 50 1 2 FIWR AN02 0 50 2 3 FIRN AN02 0 50 3 4 FICO AN02 0 50 4 5 JPWR AN02 0 70
Any sort of assistance or help is much appreciated.

Thank you in advance.
Reply
#2
Hi All,

Was able to figure it out using the stackoverflow. This what I came up with...

import pandas as pd
import openpyxl
import xlrd

writer = ExcelWriter('D:\Testing_MIS\Auto_Task\Data.xlsx')

for idx,row in Rto_Code.iterrows():
    code=row["RTO"]
    for idx,row in Rto_Main_Sheet.iterrows():
        Rto_Main_Sheet.at[idx,'RTO Code'] = code
    Rto_Main_Sheet.to_excel(writer,sheet_name=code)
    writer.save()
I'm sure there is much better way to do this.. If anyone can think of one please post.

Thank you in advance.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  FutureWarning: The behavior of DataFrame concatenation with empty or all-NA entries sawtooth500 14 260 44 minutes ago
Last Post: FortuneCoins
  Elegant way to apply each element of an array to a dataframe? sawtooth500 7 406 Mar-29-2024, 05:51 PM
Last Post: deanhystad
  Dataframe copy warning sawtooth500 4 341 Mar-25-2024, 11:38 PM
Last Post: sawtooth500
  FutureWarning: The behavior of DataFrame concatenation with empty or all-NA entries i sawtooth500 3 1,087 Mar-22-2024, 03:08 AM
Last Post: deanhystad
  Adding PD DataFrame column bsben 2 315 Mar-08-2024, 10:46 PM
Last Post: deanhystad
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 734 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  Comparing Dataframe to String? RockBlok 2 406 Nov-24-2023, 04:55 PM
Last Post: RockBlok
  Filter data into new dataframe as main dataframe is being populated cubangt 8 1,003 Oct-23-2023, 12:43 AM
Last Post: cubangt
  DataFRame.concat() nafshar 3 779 Jul-14-2023, 04:41 PM
Last Post: nafshar
  Convert dataframe from str back to datafarme Creepy 1 633 Jul-07-2023, 02:13 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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