如何在具有检查条件的数据帧中添加额外的列?

我有将数据帧分成两行的问题。我有以下两个数据帧:


df1 = pd.DataFrame({'Item':['A','B','C'],'Value':[4,3,7]})

df2 = pd.DataFrame({'Item':['A','B','C'],'Value':[4,3,2]})


df1:

    Item    Value

0      A        4

1      B        3

2      C        7


df2:

    Item    Value

0      A        4

1      B        3

2      C        2

我想为该值设置一个限制,假设限制 = 10


limit = 10

之后,我想添加一个额外的列并检查项目是否在限制范围内,如果没有,我想将其分成两行并显示以下结果:但是,如果它在限制范围内,我想添加一行以显示限制还剩下多少数量:


df1:

    Item    Value   within_limit

0      A        4            Yes

1      B        3            Yes

2      C        3            Yes

3      C        4             No


df2:

    Item    Value   within_limit

0      A        4            Yes

1      B        3            Yes

2      C        2            Yes

3    Nan        1          Extra

如何获得上述结果?


达令说
浏览 114回答 1
1回答

牧羊人nacy

这是您的函数,这假设索引是范围索引:def check_limit(df, limit):&nbsp; &nbsp; s = df['Value'].cumsum()&nbsp; &nbsp; df['within_limit'] = np.where(s.le(limit), 'Yes', 'No')&nbsp; &nbsp; # Nothing off limit&nbsp; &nbsp; if s.iloc[-1] < limit:&nbsp; &nbsp; &nbsp; &nbsp; return df.append(pd.Series([np.nan, limit-s.iloc[-1],'Extra'],&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index=df.columns,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name=len(df))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; # all sum even to limit&nbsp; &nbsp; if s.iloc[-1] == limit: return df&nbsp; &nbsp; # find where the limit is exceeded&nbsp; &nbsp; idx = s.gt(limit).idxmax()&nbsp; &nbsp; # exceed limit&nbsp; &nbsp; exceed_limit = s.loc[idx] - limit&nbsp; &nbsp; new_df = df.loc[[idx,idx]].copy()&nbsp; &nbsp; new_df['Value'] = [df.loc[idx,'Value'] - exceed_limit, exceed_limit]&nbsp; &nbsp; new_df['within_limit'] = ['Yes','No']&nbsp; &nbsp; return pd.concat((df.drop(idx), new_df)).sort_index().reset_index(drop=True)# test datadf1 = pd.DataFrame({'Item':['A','B','C', 'D'],'Value':[4,3,7,2]})df2 = pd.DataFrame({'Item':['A','B','C'],'Value':[4,3,2]})输出:# check_limit(df1, 10)&nbsp; Item&nbsp; Value within_limit0&nbsp; &nbsp; A&nbsp; &nbsp; &nbsp; 4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Yes1&nbsp; &nbsp; B&nbsp; &nbsp; &nbsp; 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Yes2&nbsp; &nbsp; C&nbsp; &nbsp; &nbsp; 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Yes3&nbsp; &nbsp; C&nbsp; &nbsp; &nbsp; 4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;No4&nbsp; &nbsp; D&nbsp; &nbsp; &nbsp; 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;No# check_limit(df2, 10)&nbsp; Item&nbsp; Value within_limit0&nbsp; &nbsp; A&nbsp; &nbsp; &nbsp; 4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Yes1&nbsp; &nbsp; B&nbsp; &nbsp; &nbsp; 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Yes2&nbsp; &nbsp; C&nbsp; &nbsp; &nbsp; 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Yes3&nbsp; NaN&nbsp; &nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp; &nbsp; Extra
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python