Feb-14-2020, 01:22 AM
Hi There,
I tried with the below code, but the results are not what I am expecting.
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.