简体   繁体   中英

Pandas - read data (two-row header, index column)

I have a data file that looks like this (miRNA-seq data from TCGA):

A X X X Y Y Y Z Z Z
B a b c a b c a b c
0
1   regular 5x9
2   data matrix
3
4

A describes the first header row, B describes the first column. I want to use pandas.read_csv to return a DataFrame such that I can access rows by something like df[0] and columns by something like df['X']['a'] , eventually delete selected rows and/or columns. On my search I have found that MultiIndex could help my, however df = pandas.read_csv("datafile", header=[0, 1]); print(df.index) df = pandas.read_csv("datafile", header=[0, 1]); print(df.index) does return an Index .

Thank you for any suggestions.

EDIT: Some sample data (tab-separated)

Hybridization REF   TCGA-2V-A95S-01A-11R-A37G-13    TCGA-2V-A95S-01A-11R-A37G-13    TCGA-2V-A95S-01A-11R-A37G-13    TCGA-2Y-A9GS-01A-12R-A38M-13    TCGA-2Y-A9GS-01A-12R-A38M-13    TCGA-2Y-A9GS-01A-12R-A38M-13    TCGA-2Y-A9GT-01A-11R-A38M-13    TCGA-2Y-A9GT-01A-11R-A38M-13    TCGA-2Y-A9GT-01A-11R-A38M-13
miRNA_ID    read_count  reads_per_million_miRNA_mapped  cross-mapped    read_count  reads_per_million_miRNA_mapped  cross-mapped    read_count  reads_per_million_miRNA_mapped  cross-mapped
hsa-let-7a-1    17377   4045.749542 N   47187   7077.368096 N   31765   8956.551210 N
hsa-let-7a-2    34913   8128.517796 N   94766   14213.530526    Y   64148   18087.355487    N
hsa-let-7a-3    17496   4073.455371 N   47683   7151.760928 N   31782   8961.344580 N
hsa-let-7b  33546   7810.249993 N   46089   6912.683963 N   64948   18312.925799    N
hsa-let-7c  1349    314.077006  N   12185   1827.573913 Y   14075   3968.627681 N
hsa-let-7d  1735    403.946335  N   1763    264.424523  N   1176    331.588359  N

Try this out:

df=pd.read_csv('zhoop.csv', header=[0,1], index_col=0)

Note: in order to index rows you would use df.loc[rownum], not just df[rownum].

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM