[英]Expanding ranking of column in Pandas
考慮一個樣本 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
嘗試以擴展方式將d1
d1
和c
列的mean
的排名。 例如,考慮以下前 5 行:
第一行默認為索引0
處的值,即Apple
將替換為0
第二行,索引1
,值Mango
應替換為0
,因為僅考慮Apple
的 DF GROUPED_MEAN
的前2
行將是 19 並且Mango
將為 17,因此索引1
處的值 Mango 應替換為 rank 0因為它具有較低的分組平均值。
第三行,索引2
,值Apple
應替換為0
,因為僅考慮Apple
的 DF GROUPED_MEAN
的前3
行將是(19+4)/2
並且Mango
將是 17,因此索引2
處的值 Apple應該用等級0
代替,因為它具有較低的分組平均值
第四行,索引3
,值Pine
應替換為2
,因為僅考慮Apple
的 DF GROUPED_MEAN
的前4
行將是(19+4)/2
並且Mango
將是 17, Pine
將是 51,因為 Pine在所有 3 個類別中具有最高的分組平均值 - [Apple, Mango, Pine]
,Pine 將獲得排名 2。
第五行,索引4
,值Apple
應替換為0
,因為僅考慮Apple
的 DF GROUPED_MEAN
的前5
行將是(19+4+8)/3
, Mango
將是 17, Pine
將是 51,由於 Apple 在所有 3- Apple, Mango, Pine
中的分組平均值最低,因此 Apple 將被評為 0 級。
d1 列的預期值:
0
0
0
2
0
0
1
0
2
1
迭代方法:
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"])
我可以通過 DF 的每一行迭代地執行此操作,但大型 DF 的性能很差,因此任何關於更多基於 pandas 的方法的建議都會很棒。
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)
結果:
# 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
表現:
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.