[pandas]pandas通过表头名称ID和index索引名称获得表头的所在列值和索引所在行值
使用pandas的get_loc方法
- pd.columns.get_loc("ID")
- pd.index.get_loc(ex_data_index)
复制代码
例子:使用iloc定位表格数值,需要得到对应表格的行值和列值而不能使用表头名称和行索引名称
- for ex_data_index in ex_data.index:
- for data_compare_content_index in data_compare_content.index:
- if ex_data_index == data_compare_content_index:
- # ex_data.columns.get_loc("ID")通过表头名称ID获得表头的所在列值
- ex_data_ID_column_num = ex_data.columns.get_loc("ID")
- ex_data_GeneSymbol_column_num = ex_data.columns.get_loc("Gene.symbol")
- ex_data_index_row_num = ex_data.index.get_loc(ex_data_index)
- data_compare_content_geneID_column_num = data_compare_content.columns.get_loc("ENTREZ_GENE_ID")
- data_compare_content_GeneSymbol_column_num = data_compare_content.columns.get_loc("Gene Symbol")
- data_compare_content_index_row_num = data_compare_content.index.get_loc(data_compare_content_index)
- ex_data.iloc[ex_data_index_row_num,ex_data_ID_column_num] = data_compare_content.iloc[data_compare_content_index_row_num,data_compare_content_geneID_column_num]
- ex_data.iloc[ex_data_index_row_num,ex_data_GeneSymbol_column_num] = data_compare_content.iloc[data_compare_content_index_row_num,data_compare_content_GeneSymbol_column_num]
复制代码 |