我有以下熊猫数据框:
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"]]
它看起来像这样:
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
我想要得到的是这样的:
gene cell1 cell2
0 foo 5 12
1 bar 9 90
2 qux 1 13
3 woz 7 87
即选择分割字符串的第二个元素//
作为分隔符。
我能做的最好的就是:
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
正确的做法是什么?