using substring and if elif condition to create new column

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ck25python
    New Member
    • Jan 2020
    • 20

    #1

    using substring and if elif condition to create new column

    Hi There,
    I am new to python so please be kind to me.
    Below is the data frame and the requirement:
    Code:
    data = {'id': ['aa11bc', 'bb22cd', 'cc33ef', 'dd44gh', 'ee55ij','ff66kl','gg77mn','hh88op'], 
            'direction': ["north, south, east, west", "north, south, east, west", "north, south/, *east, \west%", "north, south, east, west",
                          "north, south, east, west","north, south, east, west","north, south, east, west"
                         ,"north, south, east, west"]}
    df = pd.DataFrame(data, columns = ['id','direction'])
    df
    
    Requirement:
    #if id (2nd and 3rd letter) in ('a1','b2') then new_col should have north as an observation from direction column
    #else if id (2nd and 3rd letter) in ('c3','d4') then new_col should have south as an observation from direction column
    #else if id (2nd and 3rd letter) in ('e5','f6') then new_col should have east as an observation from direction column
    #else if id (2nd and 3rd letter) in ('g7','h8') then new_col should have west as an observation from direction column
    I tried with the below code: but the output is not correct it's not meeting the desired output.

    Code:
    new_col=[]
     
    for i in df["id"]:
        if i[1:3].lower()in ('a1','b2'):
            new_col=df["direction"].str.split(',').str[0].str.replace('\W+',' ').str.strip()
        elif i[1:3].lower()in ('c3','d4'):
            new_col=df["direction"].str.split(',').str[1].str.replace('\W+',' ').str.strip()
        elif i[1:3].lower()in ('e5','f6'):
            new_col=df["direction"].str.split(',').str[2].str.replace('\W+',' ').str.strip()
        elif i[1:3].lower()in ('g7','h8'):
            new_col=df["direction"].str.split(',').str[3].str.replace('\W+',' ').str.strip()
             
    df["position"]=new_col
    print(df)
    
    And the output as follows:
           id                     direction position
    0  aa11bc      north, south, east, west     west
    1  bb22cd      north, south, east, west     west
    2  cc33ef  north, south/, *east, \west%     west
    3  dd44gh      north, south, east, west     west
    4  ee55ij      north, south, east, west     west
    5  ff66kl      north, south, east, west     west
    6  gg77mn      north, south, east, west     west
    7  hh88op      north, south, east, west     west
    Please advise.
  • SioSio
    Contributor
    • Dec 2019
    • 272

    #2
    I didn't understand what you wanted to do, but is that OK?
    Do you need to replace non-alphabets in direction?
    Code:
    new_col=[]
    j = 0
    for i in df["id"]:
        if i[1:3].lower()in ('a1','b2'):
            new_col.append(re.sub(r'\W+','',df.loc[j, 'direction'].split(',')[0]))
        elif i[1:3].lower()in ('c3','d4'):
            new_col.append(re.sub(r'\W+','',df.loc[j, 'direction'].split(',')[1]))
        elif i[1:3].lower()in ('e5','f6'):
            new_col.append(re.sub(r'\W+','',df.loc[j, 'direction'].split(',')[2]))
        elif i[1:3].lower()in ('g7','h8'):
            new_col.append(re.sub(r'\W+','',df.loc[j, 'direction'].split(',')[3]))
        j = j + 1
    df ["position"] = new_col
    print(df)

    Comment

    • ck25python
      New Member
      • Jan 2020
      • 20

      #3
      Hi There,
      Thanks for the help.
      Yes, I am getting the desired output from your code.
      I want to try one more condition from your code, I let you know the status. Until such time I will keep this question as open.
      Hope this is fine with you.
      Once again thanks for your help.
      Cheers!!

      Comment

      • ck25python
        New Member
        • Jan 2020
        • 20

        #4
        HI SioSio,
        Thanks for the help with the code.

        Comment

        Working...