在数据帧列上循环时?

我有一个由两列组成的小数据帧,一列是ORG列和一列百分比列。数据帧根据百分比列从大到小排序。


我想创建一个 while 循环,将百分比列中的值相加,直到达到 .80 (80%) 的值。


到目前为止,我已经尝试过:


retail_pareto = 0

counter = 0

while retail_pareto < .80:

    retail_pareto += retailerDF[counter]['RETAILER_PCT_OF_CHANGE']

    counter += 1

这不起作用,计数器和计数器以及retail_pareto值都保持在零,没有真正的错误消息来帮助我解决我做错了什么。理想情况下,我想得到一个列表,列出加起来加起来占80%的最大百分比的组织。


我不确定下一步该尝试什么。我已经搜索了这些论坛,但还没有在论坛中找到类似的东西。


任何建议或帮助都非常感谢。谢谢。


Example Dataframe:

ORG    PCT

KST    0.582561

ISL    0.290904

BOV    0.254456

BRH    0.10824

GNT    0.0913631

DSH    0.023441

RDM    -0.0119665

JBL    -0.0348893

JBD    -0.071883

WEG    -0.232227




The output that I would expect would be something along the lines of:

ORG    PCT

KST    0.582561

ISL    0.290904


人到中年有点甜
浏览 145回答 3
3回答

一只名叫tom的猫

你能用这个例子来帮助你吗?import pandas as pdretail_pareto = 0orgs = []for i,row in retailerDF.iterrows():&nbsp; &nbsp; if retail_pareto <= .80:&nbsp; &nbsp; &nbsp; &nbsp; retail_pareto += row['RETAILER_PCT_OF_CHANGE']&nbsp; &nbsp; &nbsp; &nbsp; orgs.append(row)&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; breaknew_df = pd.DataFrame(orgs)编辑:使其更像您的示例,并添加了新的数据帧。

皈依舞

而不是循环,采取一种更乐观的方法。首先计算包含累积RETAILER_PCT_OF_CHANGE总和的附加列:df['pct_cum'] = df.RETAILER_PCT_OF_CHANGE.cumsum()对于您的数据,结果是:&nbsp; &nbsp;ORG&nbsp; RETAILER_PCT_OF_CHANGE&nbsp; &nbsp;pct_cum0&nbsp; KST&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0.582561&nbsp; 0.5825611&nbsp; ISL&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0.290904&nbsp; 0.8734652&nbsp; BOV&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0.254456&nbsp; 1.1279213&nbsp; BRH&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0.108240&nbsp; 1.2361614&nbsp; GNT&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0.091363&nbsp; 1.3275245&nbsp; DSH&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0.023441&nbsp; 1.3509656&nbsp; RDM&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-0.011967&nbsp; 1.3389997&nbsp; JBL&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-0.034889&nbsp; 1.3041098&nbsp; JBD&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-0.071883&nbsp; 1.2322269&nbsp; WEG&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-0.232227&nbsp; 0.999999现在,要打印完全包含 80% 更改的行,以超出限制的第一行结束,请运行:df[df.pct_cum.shift(1).fillna(0) < 0.8]结果与累积的总和一起为:&nbsp; &nbsp;ORG&nbsp; RETAILER_PCT_OF_CHANGE&nbsp; &nbsp;pct_cum0&nbsp; KST&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0.582561&nbsp; 0.5825611&nbsp; ISL&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0.290904&nbsp; 0.873465

红糖糍粑

用: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),:]&nbsp;print(df_filtered)&nbsp; &nbsp;ORG&nbsp; &nbsp; &nbsp; &nbsp;PCT0&nbsp; KST&nbsp; 0.5825611&nbsp; ISL&nbsp; 0.290904
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python