I have dataframe like below to which I want apply an sql logic which mentioned below
df.head(25)
ORDER_ID CODE STATUS_DATE RNK
19837715 0400 22/10/19 08:11:08.000000000 AM GMT 2
19837715 0400 22/10/19 10:00:03.000000000 AM GMT 1
19837715 0400 22/10/19 10:47:08.000000000 AM GMT 3
19837715 0500 22/10/19 10:00:00.000000000 AM GMT 1
19837715 1100 01/11/19 10:02:00.000000000 AM GMT 1
19837715 1240 02/11/19 08:00:00.000000000 AM GMT 1
19837833 0400 22/10/19 08:13:09.000000000 AM GMT 3
19837833 0400 22/10/19 08:22:09.000000000 AM GMT 4
19837833 0400 23/10/19 04:30:10.000000000 AM GMT 1
19837833 0400 23/10/19 09:30:07.000000000 PM GMT 2
19837833 0500 23/10/19 01:08:00.000000000 AM GMT 1
19837833 0500 23/10/19 04:30:00.000000000 AM GMT 3
19840750 0500 23/10/19 12:30:00.000000000 PM GMT 1
19840750 1100 01/11/19 10:06:02.000000000 AM GMT 1
19840750 1240 02/11/19 08:40:05.000000000 AM GMT 1
19840750 1305 05/11/19 07:21:03.000000000 AM GMT 2
19840750 1305 05/11/19 08:22:03.000000000 AM GMT 1
19840750 1400 09/11/19 06:13:12.000000000 AM GMT 3
I want to apply the below sql logic on this dataframe.
select
order_id
, TRUNC(MAX(decode(df.code, '0400', STATUS_DATE, Null))) act_0400
, TRUNC(MAX(decode(df.code, '0500', STATUS_DATE, Null))) act_0500
from
dataframe df
where
df.rnk =1
group by
order_id
Here I am trying to create new columns act_0400 and act_0500 by taking maximum date value from the status date column for condition rank =1 and grouping them based on order id
Expected Output
ORDER_ID ACT_0400 ACT_0500
19837715 22/10/2019 22/10/2019
19837833 23/10/2019 23/10/2019
19840750 23/10/2019
How could this be done in pandas
You can first convert STATUS_DATE
to datetimes by to_datetime
with Series.dt.date
, then filter by boolean indexing
with Series.isin
and last reshape by DataFrame.pivot_table
with aggregate max
, last some data cleaning by DataFrame.rename_axis
, DataFrame.rename_axis
and DataFrame.reset_index
:
df['STATUS_DATE'] = pd.to_datetime(df['STATUS_DATE']).dt.date
df = (df[(df['RNK'] == 1) & df['CODE'].isin([400,500])]
.pivot_table(index="ORDER_ID", columns="CODE", values="STATUS_DATE", aggfunc='max')
.rename_axis(None, axis=1)
.add_prefix('ACT_')
.reset_index())
print (df)
ORDER_ID ACT_400 ACT_500
0 19837715 2019-10-22 2019-10-22
1 19837833 2019-10-23 2019-10-23
2 19840750 NaN 2019-10-23
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加