简体   繁体   English

从每个客户 ID 的下一列中识别最后一个是值和 0,然后从前一行下一列熊猫中获取值

[英]Identify the last yes value & 0 from next column for each customer id then get the value from previous row next column pandas

Input:输入:

df = pd.DataFrame([[101,'y', 1, 'reg'],
                [101,'y', 1, '1098'],
                [101, 'y',0, 'Reg'],
                [101, 'y',0, 'sed'],
                [101,'n',0,'VA'],
                [102, 'y',1, 'Paymode'],
                [102, 'y',0, 'roy'],
                [102, 'n',0, 'Reg'],
                [103, 'y',1, 'reg'],
                [103, 'n',0, 'PCA'],
                [103, 'n',0, 'FXD']
              ]
              , columns=['cus_ID', 'emailflg','Paperlessmode', 'types of paper'])

output:输出:

df= pd.DataFrame([[101,'y', 1, 'reg','1098'],
                [101,'y', 1, '1098','1098'],
                [101, 'y',0, 'Reg','1098'],
                [101, 'y',0, 'sed','1098'],
                [101,'n',0,'VA','1098'],
                [102, 'y',1, 'Paymode','roy'],
                [102, 'y',0, 'roy','roy'],
                [102, 'n',0, 'Reg','roy'],
                [103, 'y',1, 'reg','reg'],
                [103, 'n',0, 'PCA','reg'],
                [103, 'n',0, 'FXD','reg']
              ]
              , columns=['cus_ID','emailflg', 'Paperlessmode', 'types of paper','last occurance_paper'])
condtion 1= df['Emailflg' =='y'] & df['Paperlessmode'].eq(0) & df['types of paper!='Reg','PCA']

if Condition 1 exits then take same row value from type of paper如果条件 1 退出,则从纸张类型中获取相同的行值
OR或者
identify the types of paper which is presence before zero in Paperlessmode and 'n' in Email flag for each customer id in Python 3.6在 Python 3.6 中为每个客户 ID 在 Paperlessmode 和 Email 标志中的“n”之前识别纸张类型
Kindly let me know if any concern and plz don't devote如果有任何问题,请让我知道,请不要投入

I don't really understand how your conditions are done, but for the solution that you should follow, it doesn't rally matter.我真的不明白你的条件是如何完成的,但对于你应该遵循的解决方案,它并不重要。 I have listed several pseudo-conditions which you can adapt to your own real conditions.我列出了几个伪条件,您可以根据自己的实际情况进行调整。 The idea is to list the conditions and choices for what the values of the new column should be.这个想法是列出新列的值应该是什么的条件和选择。 In this example, 4 conditions, 4 potential values.在本例中,4 个条件,4 个潜在值。 Then use np.select in the following way:然后按以下方式使用np.select

conditions = [((df['emailflg'] =='y') & (df['Paperlessmode']==0)), 
               ((df['types of paper'] =='reg') & (df['Paperlessmode']==0)),
             ((df['types of paper'] =='Reg') & (df['Paperlessmode']==0)),
              ((df['types of paper'] =='VA') & (df['Paperlessmode']==0))]
choices = ['REG','reg','Paymode', 'VA']


import numpy as np

df['last occurance_paper'] = np.select(conditions, choices, default='XXX')

which gives这使

   cus_ID emailflg  Paperlessmode types of paper last occurance_paper
0     101        y              1            reg                  XXX
1     101        y              1           1098                  XXX
2     101        y              0            Reg                  REG
3     101        n              0             VA                   VA
4     102        y              1        Paymode                  XXX
5     102        n              0            Reg              Paymode
6     103        y              1            reg                  XXX
7     103        n              0            PCA                  XXX
8     103        n              0            FXD                  XXX

I think you need:我认为你需要:

m1 = df['Paperlessmode'].eq(0)
m2 = df['emailflg'].eq('y')
m3 = df['emailflg'].eq('n') 
 
d = df[m1 & m2].set_index('cus_ID')['types of paper'].to_dict()
s = df[(m1 & m3).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(d).fillna(df['cus_ID'].map(s))
print (df)
       cus_ID emailflg  Paperlessmode types of paper last occurance_paper
0     101        y              1            reg                  Reg
1     101        y              1           1098                  Reg
2     101        y              0            Reg                  Reg
3     101        n              0             VA                  Reg
4     102        y              1        Paymode              Paymode
5     102        n              0            Reg              Paymode
6     103        y              1            reg                  reg
7     103        n              0            PCA                  reg
8     103        n              0            FXD                  reg
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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从每个客户 ID 中识别零值,然后从前一行下一行 Pandas 中获取值 - identify zero values from each customer id then get the value from previous row next column pandas Pandas:根据下一行的值替换组中的列值 - Pandas: Replace column values in a group by based on value from the next row Pandas:如何获取一列中每个项目的最后每日值并从每行中的值中减去它 - Pandas: How to get last daily value for each item in one column and subtract it from the value in each row 通过比较当前行列与熊猫中的下一行列来获取最小日期值 - Get minimum date value from comparison of current row colum vs next row column in pandas 使用 for 循环将一行(熊猫)与下一行进行比较,如果不同,则从列中获取值 - Compare a row (pandas) with the next row using for loop, and if not the same get a value from a column 如何从下一列中选择一个熊猫值 - How to select a pandas value from the next column 从 pandas df 获取上一行/特定列的值 - Get value of previous row / specific column from a pandas df 将最后一个字从一列移动到下一行 Pandas Dataframe - Move the last word from one column to next row in Pandas Dataframe Pandas:将下一列值与上一列值进行比较 - Pandas: Compare next column value with previous column value 我正在尝试从上一行和下一行的值中减去一个值 - I am trying to subtract a value from a value in the previous row and next column over
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM