I have the following pandas data frame:
import pandas as pd
df = pd.DataFrame({ 'gene':["1 // foo // blabla",
"2 // bar // lalala",
"3 // qux // trilil",
"4 // woz // hohoho"], 'cell1':[5,9,1,7], 'cell2':[12,90,13,87]})
df = source_df[["gene","cell1","cell2"]]
It looks like this:
gene cell1 cell2
0 1 // foo // blabla 5 12
1 2 // bar // lalala 9 90
2 3 // qux // trilil 1 13
3 4 // woz // hohoho 7 87
What I want to get is this:
gene cell1 cell2
0 foo 5 12
1 bar 9 90
2 qux 1 13
3 woz 7 87
Namely select 2nd element of the splited string by //
as delimiter.
The best I can do is this:
df["gene"] = df["gene"].str.split(" // ")
df
Out[17]:
gene cell1 cell2
0 [1, foo, blabla] 5 12
1 [2, bar, lalala] 9 90
2 [3, qux, trilil] 1 13
3 [4, woz, hohoho] 7 87
What's the right way to do it?