我有以下的pandas数据框:
import pandas as pd
import numpy as np
df = pd.DataFrame({
'fc': [100,100,112,1.3,14,125],
'sample_id': ['S1','S1','S1','S2','S2','S2'],
'gene_symbol': ['a', 'b', 'c', 'a', 'b', 'c'],
})
df = df[['gene_symbol', 'sample_id', 'fc']]
df
这将产生以下结果:
Out[11]:
gene_symbol sample_id fc
0 a S1 100.0
1 b S1 100.0
2 c S1 112.0
3 a S2 1.3
4 b S2 14.0
5 c S2 125.0
我该如何扩展sample_id
,以便最终获得这样的结果:
gene_symbol S1 S2
a 100 1.3
b 100 14.0
c 112 125.0
gene_symbol
作为索引还是作为列? - jezraelimport pandas as pd data = {"sample_id": [1, 2, 3], "score": [85.6, 90.4, 87.3]} df = pd.DataFrame(data) print(df)
翻译样例代码 分数 1 85.6 2 90.4 3 87.3
- neversaint