我有以下CSV数据
id,gene,celltype,stem,stem,stem,bcell,bcell,tcell
id,gene,organs,bm,bm,fl,pt,pt,bm
id,gene,organs,stem1,stem2,stem3,b1,b2,t1
134,foo,about_foo,20,10,11,23,22,79
222,bar,about_bar,17,13,55,12,13,88前三行是标题。我要做的是选择第1行和第3行,并将其转换为如下所示的数据框架:
Coln1 Coln2
stem  stem1
stem  stem2
stem  stem3
bcell b1
bcell b2
tcell t1我被困在以下几个方面:
import pandas as pd
df = pd.read_csv("http://dpaste.com/00AWDBW.txt",header=None,index_col=[1,2]).iloc[:, 1:]发布于 2016-01-20 08:38:44
您可以在nrows中使用参数read_csv和skiprows
import pandas as pd
import io
temp=u"""id,gene,celltype,stem,stem,stem,bcell,bcell,tcell
id,gene,organs,bm,bm,fl,pt,pt,bm
id,gene,organs,stem1,stem2,stem3,b1,b2,t1
134,foo,about_foo,20,10,11,23,22,79
222,bar,about_bar,17,13,55,12,13,88"""
#after testing replace io.StringIO(temp) to filename
df = pd.read_csv(io.StringIO(temp),header=None,index_col=[1,2], nrows=2, skiprows=[1])
df = df.ix[:, 1:].reset_index(drop=True).T
df.columns = ['Coln1', 'Coln2']
print df.reset_index(drop=True)
   Coln1  Coln2
0   stem  stem1
1   stem  stem2
2   stem  stem3
3  bcell     b1
4  bcell     b2
5  tell     t1若要将头3标题选择到列中,请执行以下操作:
df = pd.read_csv(io.StringIO(temp),header=None,index_col=[1,2], nrows=3, skiprows=[4])
df = df.ix[:, 1:].reset_index(drop=True).T
df.columns = ['Coln1', 'Coln2','Coln3']
print df.reset_index(drop=True)https://stackoverflow.com/questions/34894909
复制相似问题