Consider a sample DF:
df = pd.DataFrame(np.random.randint(0,60,size=(10,3)),columns=["a","b","c"])
df["d1"]=["Apple","Mango","Apple","Apple","Mango","Mango","Apple","Mango","Apple","Apple"]
df["d2"]=["Orange","lemon","lemon","Orange","lemon","Orange","lemon","Orange","lemon","Orange"]
df["date"] = ["2002-01-01","2002-01-01","2002-01-01","2002-01-01","2002-02-01","2002-02-01","2002-02-01"]
df["date"] = pd.to_datetime(df["date"])
df
a b c d1 d2 date
0 7 1 19 Apple Orange 2002-01-01
1 3 7 17 Mango lemon 2002-01-01
2 9 6 4 Apple lemon 2002-01-01
3 0 5 51 Pine Orange 2002-01-01
4 4 6 8 Apple lemon 2002-02-01
5 4 3 1 Mango Orange 2002-02-01
6 2 2 14 Apple lemon 2002-02-01
7 5 15 10 Mango Orange 2002-01-01
8 1 2 10 Pine lemon 2002-02-01
9 2 1 12 Apple Orange 2002-02-01
Trying to replace column d1
with rank based on Group by column d1
and mean
of column c
in expanding manner. For example, consider the following first 5 rows:
First row by default the value at index 0
, ie Apple
will be replaced with 0
Second row, index 1
, the value Mango
should be replaced by 0
, because considering only the first 2
rows of the DF GROUPED_MEAN
for Apple
will be 19 and Mango
will be 17, so the value Mango at index 1
should be replaced by rank 0 since it has lower grouped mean.
Third row, index 2
, the value Apple
should be replaced by 0
, because considering only the first 3
rows of the DF GROUPED_MEAN
for Apple
will be (19+4)/2
and Mango
will be 17, so the value Apple at index 2
should be replaced by rank 0
since it has lower grouped mean
Fourth row, index 3
, the value Pine
should be replaced by 2
, because considering only the first 4
rows of the DF GROUPED_MEAN
for Apple
will be (19+4)/2
and Mango
will be 17, Pine
will be 51, since Pine has the highest grouped mean of all the 3 categories- [Apple, Mango, Pine]
, Pine will be given rank 2.
Fifth row, index 4
, the value Apple
should be replaced by 0
, because considering only the first 5
rows of the DF GROUPED_MEAN
for Apple
will be (19+4+8)/3
and Mango
will be 17, Pine
will be 51, since Apple has the lowest grouped mean of all the 3 - Apple, Mango, Pine
, Apple will be given rank 0.
Expected Value of column d1:
0
0
0
2
0
0
1
0
2
1
Iterative Approach:
def expanding(data,cols):
copy_df = data.copy(deep=True)
for i in range(len(copy_df)):
if i==0:
copy_df.loc[i,cols]=0
else:
op = group_processor(data[:i+1],cols,i)
copy_df.loc[i,cols]=op
return copy_df
def group_processor(cut_df,cols,i):
op=[]
for each_col in cols:
temp = cut_df.pivot_table("c",[each_col]).rank(method="dense")-1
value = cut_df.loc[i,each_col]
temp = temp.reset_index()
final_value = temp.loc[temp[each_col]==value,"c"]
op.append(final_value.values[0])
return op
expanding(df,["d1"])
I am able do this iteratively through every row of the DF, but the performance is poor for large DFs so any suggestions on a more pandas based approach will be great.
Use Series.expanding
with minimum window size of 1
on column c
, and use a custom lambda function exp
. In this lambda function we use Series.groupby
to group the exapnding window w
by the column d1
in the original dataframe and transform
using mean
, finally using Series.rank
with method='dense'
we calculate the rank:
exp = lambda w: w.groupby(df['d1']).transform('mean').rank(method='dense').iat[-1]
df['d1_new'] = df['c'].expanding(1).apply(exp).sub(1).astype(int)
Result:
# print(df)
a b c d1 d2 date d1_new
0 7 1 19 Apple Orange 2002-01-01 0
1 3 7 17 Mango lemon 2002-01-01 0
2 9 6 4 Apple lemon 2002-01-01 0
3 0 5 51 Pine Orange 2002-01-01 2
4 4 6 8 Apple lemon 2002-02-01 0
5 4 3 1 Mango Orange 2002-02-01 0
6 2 2 14 Apple lemon 2002-02-01 1
7 5 15 10 Mango Orange 2002-01-01 0
8 1 2 10 Pine lemon 2002-02-01 2
9 2 1 12 Apple Orange 2002-02-01 1
Performance:
df.shape
(1000, 7)
%%timeit
exp = lambda w: w.groupby(df['d1']).transform('mean').rank(method='dense').iat[-1]
df['d1_new'] = df['c'].expanding(1).apply(exp).sub(1).astype(int)
3.15 s ± 305 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%%timeit
expanding(df,["d1"]) # your method
11.9 s ± 449 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.