I am facing a problem regarding separation of path of files in a csv file. I have such a structure in a csv file:
w:b/c/v/n/1/y/i
w:b/c/v/n/2/h/l
w:b/c/v/n/3/n/r
w:b/c/v/n/4/f/e
this is one column of my csv file as the path name. Now what I need to do is to preserve my first column and create 3 more columns for:
1/ y /i
2 h l
3 n r
4 f e
I know that .str.split('/', expand=True)
works in such a cases but my problem is how to show that it should leave out "b/c/v/n" part. Could you please help me with it?
CodePudding user response:
If you have DataFrame like this:
path
0 w:b/c/v/n/1/y/i
1 w:b/c/v/n/2/h/l
2 w:b/c/v/n/3/n/r
3 w:b/c/v/n/4/f/e
Then:
df[["col1", "col2", "col3"]] = (
df["path"].str.split("/", expand=True).iloc[:, -3:]
)
print(df)
creates 3 new columns:
path col1 col2 col3
0 w:b/c/v/n/1/y/i 1 y i
1 w:b/c/v/n/2/h/l 2 h l
2 w:b/c/v/n/3/n/r 3 n r
3 w:b/c/v/n/4/f/e 4 f e
CodePudding user response:
You could first get a slice of the string and then use split like the following:
.str[10:].str.split('/', expand=True)
CodePudding user response:
Another try is:
s = 'w:b/c/v/n/1/y/i'
temp = s.split("/")
s = "/".join(temp[0:4]) "," ",".join(temp[4:7])
Result is:
w:b/c/v/n,1,y,i