I have a dataframe column with a strings representing a paths. I'd like to use some of that path as the value in another column.
The strings are similar to the following and in a Column Titled 'Image Location'
C:\Users\Chris H\Desktop\20161017HCT116\Day 4\D2\Image9.tif
C:\Users\Chris H\Desktop\20161017HCT116\Day 4\D6\Image7.tif
C:\Users\Chris H\Desktop\20161017HCT116\Day 4\D7\Image3.tif
...
C:\Users\Chris H\Desktop\20161017HCT116\Day 6\D2\Image7.tif
C:\Users\Chris H\Desktop\20161017HCT116\Day 6\D2\Image1.tif
C:\Users\Chris H\Desktop\20161017HCT116\Day 6\D2\Image6.tif
C:\Users\Chris H\Desktop\20161017HCT116\Day 6\D3\Image4.tif
C:\Users\Chris H\Desktop\20161017HCT116\Day 6\D3\Image9.tif
...
C:\Users\Chris H\Desktop\20161017HCT116\Day 8\D1\Image4.tif
C:\Users\Chris H\Desktop\20161017HCT116\Day 8\D1\Image9.tif
C:\Users\Chris H\Desktop\20161017HCT116\Day 8\D1\Image3.tif
C:\Users\Chris H\Desktop\20161017HCT116\Day 8\D2\Image7.tif
C:\Users\Chris H\Desktop\20161017HCT116\Day 8\D2\Image1.tif
C:\Users\Chris H\Desktop\20161017HCT116\Day 8\D2\Image6.tif
Right now I'm doing the following :
df['Interval'] = df['Image Location'].str.split('\\').apply(lambda x: x[5])
df['Device'] = df['Image Location'].str.split('\\').apply(lambda x: x[6])
This clearly requires the path not to change very much because I'm counting the number of \ to find the Interval and Device values.
I'm wondering if there's a more robust way to do this. For instance, maybe find a pattern such as Day # and D# Any thoughts would be appreciated.