简体   繁体   English

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

[英]identify zero values from each customer id then get the value from previous row next column pandas

Input:输入:

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'])

output:输出:

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'])

I want to identify the types of paper which is presence before zero in Paperlessmode for each customer id in Python 3.6我想为 Python 3.6 中的每个客户 ID 识别在 Paperlessmode 中出现在零之前的纸张类型

You can use Series.map by shifted values with cumulative sum and compared by 1 :您可以使用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

Alternative:选择:

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)

暂无
暂无

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

相关问题 从每个客户 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 从值相对于前一列中的值减小的列开始,用零填充 dataframe 的每一行 - fill each row of a dataframe with zero starting from the column in which the values decreases with respect to the value in the previous column 从 pandas df 中的下一行获取值并插入到上一行 - get values from next row in pandas df and insert to previous row 从 pandas dataframe 的列中获取上一个和下一个三个值 - Get the previous and next three values from an column of pandas dataframe Pandas:根据下一行的值替换组中的列值 - Pandas: Replace column values in a group by based on value from the next row 从 pandas df 获取上一行/特定列的值 - Get value of previous row / specific column from a pandas df 在熊猫中将字符串值从上一行添加到下一行 - Adding string values from previous row to next row in Pandas Pandas:从ID中获取行,该ID是列值的分割 - Pandas : Get row from an ID which is a split of a column value 使用 pandas 确定上个月的客户在本月是否有销售 - Identify if customer from previous month has sales in this month using pandas 从Pandas列中的当前行值中减去前一行的值 - Subtract previous row value from the current row value in a Pandas column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM