3 回答

TA貢獻(xiàn)1906條經(jīng)驗(yàn) 獲得超3個(gè)贊
你能用這個(gè)例子來(lái)幫助你嗎?
import pandas as pd
retail_pareto = 0
orgs = []
for i,row in retailerDF.iterrows():
if retail_pareto <= .80:
retail_pareto += row['RETAILER_PCT_OF_CHANGE']
orgs.append(row)
else:
break
new_df = pd.DataFrame(orgs)
編輯:使其更像您的示例,并添加了新的數(shù)據(jù)幀。

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超3個(gè)贊
而不是循環(huán),采取一種更樂(lè)觀的方法。首先計(jì)算包含累積RETAILER_PCT_OF_CHANGE總和的附加列:
df['pct_cum'] = df.RETAILER_PCT_OF_CHANGE.cumsum()
對(duì)于您的數(shù)據(jù),結(jié)果是:
ORG RETAILER_PCT_OF_CHANGE pct_cum
0 KST 0.582561 0.582561
1 ISL 0.290904 0.873465
2 BOV 0.254456 1.127921
3 BRH 0.108240 1.236161
4 GNT 0.091363 1.327524
5 DSH 0.023441 1.350965
6 RDM -0.011967 1.338999
7 JBL -0.034889 1.304109
8 JBD -0.071883 1.232226
9 WEG -0.232227 0.999999
現(xiàn)在,要打印完全包含 80% 更改的行,以超出限制的第一行結(jié)束,請(qǐng)運(yùn)行:
df[df.pct_cum.shift(1).fillna(0) < 0.8]
結(jié)果與累積的總和一起為:
ORG RETAILER_PCT_OF_CHANGE pct_cum
0 KST 0.582561 0.582561
1 ISL 0.290904 0.873465

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超6個(gè)贊
用:
df_filtered = df.loc[df['PCT'].shift(fill_value=0).cumsum().le(0.80),:]
#if you don't want include where cumsum is greater than 0,80
#df_filtered = df.loc[df['PCT'].cumsum().le(0.80),:]
print(df_filtered)
ORG PCT
0 KST 0.582561
1 ISL 0.290904
添加回答
舉報(bào)