Python Forum
substring function to create new column
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
substring function to create new column
#1
Hi There,
I am new to python so please be kind to me.
Below is the data frame and the requirement:

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
Appreciate if you guys can assist me with this.
Reply


Messages In This Thread
substring function to create new column - by Chandan - Feb-13-2020, 09:19 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Create a function vector Cooketaker 4 995 Dec-27-2022, 08:22 PM
Last Post: Cooketaker
  Filter data based on a value from another dataframe column and create a file using lo pawanmtm 1 4,289 Jul-15-2020, 06:20 PM
Last Post: pawanmtm
  How to create a random library for an specific function andre_fermart 4 3,329 Apr-10-2019, 11:02 PM
Last Post: andre_fermart
  Create selection box to pass string value based on uniques in Excel column sneakysnek 1 2,508 Nov-18-2018, 07:29 PM
Last Post: Stefanovietch

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020