Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 具有第一个零和'的条件;n';然后从每个客户id中获取字母名称并添加到下一列_Python 3.x_Pandas - Fatal编程技术网

Python 3.x 具有第一个零和'的条件;n';然后从每个客户id中获取字母名称并添加到下一列

Python 3.x 具有第一个零和'的条件;n';然后从每个客户id中获取字母名称并添加到下一列,python-3.x,pandas,Python 3.x,Pandas,输入: 输出: wdf = 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, 'Paym

输入:

输出:

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

我想用Python 3.6识别每个客户id的前零和无纸模式中的“n”中存在的纸张类型。然后,您可以通过以下方式进行分组:


或者使用
删除重复项
映射

print(wdf)
    cus_ID emailflg  Paperlessmode types of paper first occurance_paper_n_0
0      101        y              1            reg                        VA
1      101        y              1           1098                        VA
2      101        y              0            Reg                        VA
3      101        y              0            sed                        VA
4      101        n              0             VA                        VA
5      102        y              1        Paymode                       Reg
6      102        y              0            roy                       Reg
7      102        n              0            Reg                       Reg
8      103        y              1            reg                       PCA
9      103        n              0            PCA                       PCA
10     103        n              0            FXD                       PCA
wdf['first occurance_paper_n_0'] = wdf['cus_ID'].map(wdf[wdf['Paperlessmode'].eq(0)
                                                      & wdf['emailflg'].eq("n")]
                                   .groupby('cus_ID')['types of paper'].first())
print(wdf)
    cus_ID emailflg  Paperlessmode types of paper first occurance_paper_n_0
0      101        y              1            reg                        VA
1      101        y              1           1098                        VA
2      101        y              0            Reg                        VA
3      101        y              0            sed                        VA
4      101        n              0             VA                        VA
5      102        y              1        Paymode                       Reg
6      102        y              0            roy                       Reg
7      102        n              0            Reg                       Reg
8      103        y              1            reg                       PCA
9      103        n              0            PCA                       PCA
10     103        n              0            FXD                       PCA
cond = wdf['Paperlessmode'].eq(0)& wdf['emailflg'].eq("n")

wdf['first occurance_paper_n_0'] = wdf['cus_ID'].map(dict(
  wdf.loc[cond,['cus_ID','types of paper']].drop_duplicates('cus_ID').to_numpy()))