Reputation: 64024
I have the following CSV data
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
The first three lines are the header. What I want to do is to select line 1 and 3 and turn it into a data frame that looks like this:
Coln1 Coln2
stem stem1
stem stem2
stem stem3
bcell b1
bcell b2
tcell t1
I am stuck with the following:
import pandas as pd
df = pd.read_csv("http://dpaste.com/00AWDBW.txt",header=None,index_col=[1,2]).iloc[:, 1:]
Upvotes: 0
Views: 63
Reputation: 862751
You can use parameters nrows
and skiprows
in read_csv
:
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
To select top 3 header into columns do this:
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)
Upvotes: 1