python - 从每个客户 ID 中识别零值,然后从前一行下一行 Pandas 中获取值

标签 python pandas dataframe

输入:

df = pd.DataFrame([[101, 1, 'reg'],
               [101, 1, '1098'],
               [101, 0, 'Reg'],
               [102, 1, 'Paymode'],
               [102, 0, 'Reg'],
               [103, 1, 'reg'],
               [103, 0.0, 'reg'],
               [103, 0.0, 'reg']
              ]
              , columns=['cus_ID', 'Paperlessmode', 'types of paper'])
输出:
df=pd.DataFrame([[101, 1, 'reg','1098'],
               [101, 1, '1098','1098'],
               [101, 0, 'Reg','1098'],
               [102, 1, 'Paymode','Paymode'],
               [102, 0, 'Reg','Paymode'],
               [103, 1, 'reg','reg'],
               [103, 0.0, 'reg','reg'],
               [103, 0.0, 'reg','reg']
              ]
              , columns=['cus_ID', 'Paperlessmode', 'types of paper','last occurance_paper'])
我想为 Python 3.6 中的每个客户 ID 识别在 Paperlessmode 中出现在零之前的纸张类型

最佳答案

您可以使用 Series.map 通过具有累积总和的移位值并通过 1 进行比较:

s = df[df['Paperlessmode'].eq(0).groupby(df['cus_ID']).transform(lambda x: x.shift(-1).cumsum().eq(1))].set_index('cus_ID')['types of paper']
df['last occurance_paper'] = df['cus_ID'].map(s)
print (df)
   cus_ID  Paperlessmode types of paper last occurance_paper
0     101            1.0            reg                 1098
1     101            1.0           1098                 1098
2     101            0.0            Reg                 1098
3     102            1.0        Paymode              Paymode
4     102            0.0            Reg              Paymode
5     103            1.0            reg                  reg
6     103            0.0            reg                  reg
7     103            0.0            reg                  reg
选择:
d = df[df['Paperlessmode'].eq(0).groupby(df['cus_ID']).shift(-1, fill_value=False)].set_index('cus_ID')['types of paper'].to_dict()
df['last occurance_paper'] = df['cus_ID'].map(d)

关于python - 从每个客户 ID 中识别零值,然后从前一行下一行 Pandas 中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64909931/

相关文章:

python - 使用 python 将新列从列表 append 到 df

python - 如何防止 pandas.Dataframe.to_latex 在 latex 公式字符(即美元符号/括号)之前添加转义\字符?

r - 如何用非事件的开始时间和结束时间填充每 2 行

python - 匹配特定模式之后的所有内容,除了空格直到下一个字母

python - 字谜Python 3

python - 标记数据框的索引

python - 在工作日范围内扩展索引

python - 如何从数据框中删除表情符号?

python - 使用 Turtle 绘制坐标

python - 如何在不支持 tk 的情况下构建 Python3?