vendredi 10 juillet 2020

how to add column and fill it values by conditions between two data frames in python

I have the following 2 data set:

Individual data

   household_id     member_id   channel  event_begin    event_end
0   1                  1          100      83098          83300
1   1                  2          100      83150          83600
2   1                  1          200      83700          83865
3   1                  2          200      83931          83963
4   1                  3          200      84367          84532
5   1                  4          450      84598          84721
6   2                  1          300      83841          83906
7   2                  2          300      78219          78500

Household data

    household_id    channel    Begin     End
  0     1             100      83098    83600
  1     1             200      84367    84532
  2     2             300      83841    83906
  3     2             300      78219    78452

I want to add column in Individual data as ['FS_NFS'] if Household data has same household_id and channel as in Individual data (i.e. Individual and Household data has same id=(household_id&channel))

Now I want put 'FS' in column 'FS_NFS' in Individual Data if the following condition satisfies

(indv['event_begin']>=HH['Begin']) & (indv['event_end']<=HH['End']) & 
       (indv['household_id']==HH['household_id']) & (indv['channel']==HH['channel'])

else I I want 'NFS' in column 'FS_NFS' in Individual data

Expected O/p:

   household_id     member_id   channel  event_begin    event_end  FS_NFS
0   1                  1          100      83098          83300      FS
1   1                  2          100      83150          83600      FS
2   1                  1          200      83700          83865      NFS 
3   1                  2          200      83931          83963      NFS 
4   1                  3          200      84367          84532      FS
5   1                  4          450      84598          84721      NFS (Channel not prsent in both)
6   2                  1          300      83841          83906      FS
7   2                  2          300      78219          78500      NFS

Aucun commentaire:

Enregistrer un commentaire