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
#3
Hi There,
I tried with the below code, but the results are not what I am expecting.

new_col=[]

for i in df["id"]:
    if i[1:3].lower()in ('a1','b2'):
        new_col=df["direction"].str.split(',').str[0]
    elif i[1:3].lower()in ('c3','d4'):
        new_col=df["direction"].str.split(',').str[1]
    elif i[1:3].lower()in ('e5','f6'):
        new_col=df["direction"].str.split(',').str[2]
    elif i[1:3].lower()in ('g7','h8'):
        new_col=df["direction"].str.split(',').str[3]
        
df["position"]=new_col
print(df)
Output:
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.
Reply


Messages In This Thread
RE: substring function to create new column - by Chandan - Feb-14-2020, 01:22 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Create a function vector Cooketaker 4 1,004 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,302 Jul-15-2020, 06:20 PM
Last Post: pawanmtm
  How to create a random library for an specific function andre_fermart 4 3,349 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,511 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