0

I have two dataframes

  1. upi_df
  2. sms_df

Dataframe sample:

cycle_end_date | triggered_sms | delivered_sms | event_date
2021-11-30 | 45 | 30 | 2021-12-01
2021-11-30 | 45 | 20 | 2021-12-02
2021-11-30 | 40 | 30 | 2021-12-03
2021-12-15| 4 | 3 | 2021-12-17
2021-12-15| 49 | 30 | 2021-12-18

All the dataframes have the same structure like this with their respective stats.

I want to create different dataframes based on cycle_end_date I wrote the following code:

df_list = [upi_df, sms_df]
for df in df_list:
    gb = df.groupby(['cycle_end_date'])
    l = []
    for i in gb.indices:
        df = pd.DataFrame(gb.get_group(i))
        l.append(df)

but when i print l[0] I get sms_df stats with its 1st cycle (which is okay) but I am wondering how do I get upi_df stats as well of 1st cycle?

expected output: this should be my first output

cycle_end_date | triggered_upi | delivered_upi | event_date
2021-11-30 | 45 | 30 | 2021-12-01
2021-11-30 | 45 | 20 | 2021-12-02
2021-11-30 | 40 | 30 | 2021-12-03

cycle_end_date | triggered_sms | delivered_sms | event_date
2021-11-30 | 45 | 30 | 2021-12-01
2021-11-30 | 45 | 20 | 2021-12-02
2021-11-30 | 40 | 30 | 2021-12-03

Any help?

For one df:

dfs_list = []
for cycle in upi_df.cycle_end_date.unique():
    temp = upi_df[upi_df.cycle_end_date==cycle]
    dfs_list.append(temp)
coder_bg
  • 340
  • 6
  • 20
  • You might be looking for something like [pd.Grouper](https://pandas.pydata.org/docs/reference/api/pandas.Grouper.html) – Sheikh Abdul Manan Apr 11 '22 at 09:06
  • 1
    Sorry I can't see what are you looking for. – Salvatore Daniele Bianco Apr 11 '22 at 09:15
  • I just need to create different dataframes based on unique cycle end dates for upi_df and sms_df. basically each df should have one cycle end date. – coder_bg Apr 11 '22 at 09:21
  • dfs_list = [] for cycle in upi_df.cycle_end_date.unique(): temp = upi_df[upi_df.cycle_end_date==cycle] dfs_list.append(temp) this way I am getting upi_df seprate dataframes. How do I create a loop for sms_df as well? – coder_bg Apr 11 '22 at 09:22
  • have edited my question, pls check? @SalvatoreDanieleBianco – coder_bg Apr 11 '22 at 09:23
  • `df_list = [g for _,g in upi_df.groupby('cycle_end_date')]`, if you need the data from another dataframe in combination, merge it first. – mozway Apr 11 '22 at 09:26

0 Answers0