[英]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.