pandas groupby with condition on one column to populate another column

TLanni

I have a following dataframe df_address containing addresses of students

student_id     address_type     Address          City      
 1                R              6th street      MPLS              
 1                P              10th street SE  Chicago           
 1                E              10th street SE  Chicago           
 2                P              Washington ST   Boston            
 2                E              Essex St        NYC               
 3                E              1040 Taft Blvd  Dallas            
 4                R              24th street      NYC             
 4                P              8th street SE  Chicago           
 5                T              10 Riverside Ave Boston
 6                                20th St          NYC          

Each student can have multiple address types:

R stands for "Residential",P for "Permanent" ,E for "Emergency",T for "Temporary" and addr_type can also be blank

I want to populate "IsPrimaryAddress" columns based on the following logic:

If for particular student if address_type R exists then "Yes" should be written in front of address_type "R" in the IsPrimaryAddress column and "No" should be written in front of other address types for that particular student_id.

if address_type R doesn't exist but P exists then IsPrimaryAddress='Yes' for 'P' and 'No' for rest of the types

if neither P or R exists,but E exists then IsPrimaryAddress='Yes' for 'E' if P,R or E don't exist,but 'T' exists then IsPrimaryAddress='Yes' for 'T' Resultant dataframe would look like this:

student_id     address_type     Address          City      IsPrimaryAddress
 1                R              6th street      MPLS              Yes
 1                P              10th street SE  Chicago           No
 1                E              10th street SE  Chicago           No
 2                P              Washington ST   Boston            Yes
 2                E              Essex St        NYC               No
 3                E              1040 Taft Blvd  Dallas            Yes
 4                R              24th street      NYC             Yes
 4                P              8th street SE  Chicago           No
 5                T              10 Riverside Ave Boston          Yes
 6                                20th St          NYC           Yes

How can I achieve this?I tried rank and cumcount functions on address_type but couldn't get them work.

BEN_YO

First using Categorical make the address_type can be sort customized

df.address_type=pd.Categorical(df.address_type,['R','P','E','T',''],ordered=True)

df=df.sort_values('address_type') # the sort the values
df['new']=(df.groupby('student_id').address_type.transform('first')==df.address_type).map({True:'Yes',False:'No'}) # since we sorted the value , so the first value of each group is the one we need to mark as Yes
df=df.sort_index() # sort the index order back to the original df


   student_id address_type  new
0           1            R  Yes
1           1            P   No
2           1            E   No
3           2            P  Yes
4           2            E   No
5           3            E  Yes
6           4            R  Yes
7           4            P   No
8           5            T  Yes
9           6               Yes

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Groupby and descendingly rank one column based on another one in Pandas

分類Dev

Copy value from one column to another based on condition (using pandas)

分類Dev

pandas groupby where you get the max of one column and the min of another column

分類Dev

Pandas: Extract acronym from substrings of one column and match it to another column with a condition

分類Dev

New column based on 1 condition using index and one column groupby

分類Dev

Sort dataframe by another on one column - pandas

分類Dev

Sort dataframe by another on one column - pandas

分類Dev

How to take a portion of one column to match with a portion of another column in pandas?

分類Dev

Python pandas find element of one column in list of elements of another column

分類Dev

Use one column of a groupby to create X new columns with pandas

分類Dev

Change one column with one of multiple strings from another column if condition is met

分類Dev

How do I groupby with one key column, using condition on 2nd column and adding third column separately for weekday and weekend?

分類Dev

Value in one OR another column

分類Dev

Slicing values in a column to make a condition for another column

分類Dev

Pandas: creating a new column conditional on substring searches of one column and inverse of another column

分類Dev

Copy values from one column to another using Pandas

分類Dev

Pandas: Sort number of words in one column by the values of another

分類Dev

Pandas: How to calculate the percentage of one column against another?

分類Dev

Pandas - transpose one column

分類Dev

Rank according to one column and grouby another column

分類Dev

Populate one column with data from a list and match other column data

分類Dev

Pandas: How to find number of unique elements for one column coming from another column?

分類Dev

pandas sort a column by values in another column

分類Dev

Pandas: Multiply a column based on contents of another column

分類Dev

Assign values in pandas column based on another column

分類Dev

Populating a column based on values in another column - pandas

分類Dev

Column to aggregate values in another one

分類Dev

How to use apply for two pandas column including lists to return index in a list in one column using the element in another column?

分類Dev

Is there a way in pandas to groupby and then count unique where another column has a specified value?

Related 関連記事

  1. 1

    Groupby and descendingly rank one column based on another one in Pandas

  2. 2

    Copy value from one column to another based on condition (using pandas)

  3. 3

    pandas groupby where you get the max of one column and the min of another column

  4. 4

    Pandas: Extract acronym from substrings of one column and match it to another column with a condition

  5. 5

    New column based on 1 condition using index and one column groupby

  6. 6

    Sort dataframe by another on one column - pandas

  7. 7

    Sort dataframe by another on one column - pandas

  8. 8

    How to take a portion of one column to match with a portion of another column in pandas?

  9. 9

    Python pandas find element of one column in list of elements of another column

  10. 10

    Use one column of a groupby to create X new columns with pandas

  11. 11

    Change one column with one of multiple strings from another column if condition is met

  12. 12

    How do I groupby with one key column, using condition on 2nd column and adding third column separately for weekday and weekend?

  13. 13

    Value in one OR another column

  14. 14

    Slicing values in a column to make a condition for another column

  15. 15

    Pandas: creating a new column conditional on substring searches of one column and inverse of another column

  16. 16

    Copy values from one column to another using Pandas

  17. 17

    Pandas: Sort number of words in one column by the values of another

  18. 18

    Pandas: How to calculate the percentage of one column against another?

  19. 19

    Pandas - transpose one column

  20. 20

    Rank according to one column and grouby another column

  21. 21

    Populate one column with data from a list and match other column data

  22. 22

    Pandas: How to find number of unique elements for one column coming from another column?

  23. 23

    pandas sort a column by values in another column

  24. 24

    Pandas: Multiply a column based on contents of another column

  25. 25

    Assign values in pandas column based on another column

  26. 26

    Populating a column based on values in another column - pandas

  27. 27

    Column to aggregate values in another one

  28. 28

    How to use apply for two pandas column including lists to return index in a list in one column using the element in another column?

  29. 29

    Is there a way in pandas to groupby and then count unique where another column has a specified value?

ホットタグ

アーカイブ