【1.4】Pandas學習—遍歷某列的所有行
阿新 • • 發佈:2019-02-13
遍歷某列的所有行
import pandas as pd df_pathway = pd.read_excel('C:/Users/Administrator.USER-20160219OS/Desktop/代謝通路富集表.xlsx',sheetname='mbrole_enrich') print(df_pathway.head(3),type(df_pathway)) print('-----------------------------------------------------------------------') sid = df_pathway['Submitted IDs'] print(sid,type(sid)) print('-----------------------------------------------------------------------') for i in df_pathway['Submitted IDs']: #遍歷某列所有行 print(i,type(i)) # i型別是字串 print(i.split(' ')) # 字串轉列表 print('------------------------------') values = pd.DataFrame(i.split(' '),columns=['id']) # 列表轉dataframe print(values)
執行結果如下:
ID Annotation Annotation Category Group \ 0 rno00564 Glycerophospholipid metabolism KEGG pathways Pathways 1 rno00100 Steroid biosynthesis KEGG pathways Pathways 2 rno00591 Linoleic acid metabolism KEGG pathways Pathways Database set in set background in background \ 0 KEGG (Rattus norvegicus (rat)) 13 3 3069 46 1 KEGG (Rattus norvegicus (rat)) 13 3 3069 51 2 KEGG (Rattus norvegicus (rat)) 13 2 3069 26 p-value -log(p-value) FDR correction Submitted IDs \ 0 0.000812 3.090444 0.00715 C00157 C00350 C04230 1 0.001100 2.958607 0.00715 C01561 C06085 C02530 2 0.005080 2.294136 0.02200 C00157 C14765 Matching IDs URL 0 C00157 C00350 C04230 https://www.genome.jp/kegg-bin/show_pathway?15... 1 C01561 C06085 C02530 https://www.genome.jp/kegg-bin/show_pathway?15... 2 C00157 C14765 https://www.genome.jp/kegg-bin/show_pathway?15... <class 'pandas.core.frame.DataFrame'> ----------------------------------------------------------------------- 0 C00157 C00350 C04230 1 C01561 C06085 C02530 2 C00157 C14765 3 C06427 C00157 4 C00350 5 C00350 6 C05790 C02191 7 C00836 8 C06007 9 C06427 10 C00157 11 C05499 12 C01561 C02191 C06427 C00157 C00350 C00836 C06007 Name: Submitted IDs, dtype: object <class 'pandas.core.series.Series'> ----------------------------------------------------------------------- C00157 C00350 C04230 <class 'str'> ['C00157', 'C00350', 'C04230'] ------------------------------ id 0 C00157 1 C00350 2 C04230 C01561 C06085 C02530 <class 'str'> ['C01561', 'C06085', 'C02530'] ------------------------------ id 0 C01561 1 C06085 2 C02530 C00157 C14765 <class 'str'> ['C00157', 'C14765'] ------------------------------ id 0 C00157 1 C14765 C06427 C00157 <class 'str'> ['C06427', 'C00157'] ------------------------------ id 0 C06427 1 C00157 C00350 <class 'str'> ['C00350'] ------------------------------ id 0 C00350 C00350 <class 'str'> ['C00350'] ------------------------------ id 0 C00350 C05790 C02191 <class 'str'> ['C05790', 'C02191'] ------------------------------ id 0 C05790 1 C02191 C00836 <class 'str'> ['C00836'] ------------------------------ id 0 C00836 C06007 <class 'str'> ['C06007'] ------------------------------ id 0 C06007 C06427 <class 'str'> ['C06427'] ------------------------------ id 0 C06427 C00157 <class 'str'> ['C00157'] ------------------------------ id 0 C00157 C05499 <class 'str'> ['C05499'] ------------------------------ id 0 C05499 C01561 C02191 C06427 C00157 C00350 C00836 C06007 <class 'str'> ['C01561', 'C02191', 'C06427', 'C00157', 'C00350', 'C00836', 'C06007'] ------------------------------ id 0 C01561 1 C02191 2 C06427 3 C00157 4 C00350 5 C00836 6 C06007