I want to keep columns of kirp_deg_gene_exp
only if the subtype
row value is either KIRP
or normal
.
df_kirp = df_kirp.sort_index()
mrna_clin = mrna_clin.sort_index()
mrna_clin = mrna_clin.iloc[:,7:]
# Keep only columns of df that has clin_type info
kirp_deg_gene_exp = mrna_clin[mrna_clin.columns.intersection(df_kirp.index)].T
kirp_deg_gene_exp = pd.concat([kirp_deg_gene_exp, subtype])
kirp_deg_gene_exp = kirp_deg_gene_exp[kirp_deg_gene_exp.loc['subtype'].str.contains("KIRP|normal")]
Traceback:
/opt/conda/lib/python3.7/site-packages/ipykernel_launcher.py:4: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
after removing the cwd from sys.path.
---------------------------------------------------------------------------
IndexingError Traceback (most recent call last)
/tmp/ipykernel_27/274904861.py in <module>
2 kirp_deg_gene_exp = mrna_clin[mrna_clin.columns.intersection(df_kirp.index)].T
3 kirp_deg_gene_exp = pd.concat([kirp_deg_gene_exp, subtype])
----> 4 kirp_deg_gene_exp = kirp_deg_gene_exp[kirp_deg_gene_exp.loc['subtype'].str.contains("KIRP|normal")]
5 kirp_deg_gene_exp
/opt/conda/lib/python3.7/site-packages/pandas/core/frame.py in __getitem__(self, key)
3447 # Do we have a (boolean) 1d indexer?
3448 if com.is_bool_indexer(key):
-> 3449 return self._getitem_bool_array(key)
3450
3451 # We are left with two options: a single key, and a collection of keys,
/opt/conda/lib/python3.7/site-packages/pandas/core/frame.py in _getitem_bool_array(self, key)
3500 # check_bool_indexer will throw exception if Series key cannot
3501 # be reindexed to match DataFrame rows
-> 3502 key = check_bool_indexer(self.index, key)
3503 indexer = key.nonzero()[0]
3504 return self._take_with_is_copy(indexer, axis=0)
/opt/conda/lib/python3.7/site-packages/pandas/core/indexing.py in check_bool_indexer(index, key)
2387 if mask.any():
2388 raise IndexingError(
-> 2389 "Unalignable boolean Series provided as "
2390 "indexer (index of the boolean Series and of "
2391 "the indexed object do not match)."
IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match).
CodePudding user response:
If subtype
is column remove loc
:
kirp_deg_gene_exp = kirp_deg_gene_exp[kirp_deg_gene_exp['subtype'].str.contains("KIRP|normal")]
If subtype
is label of index
add another loc
:
kirp_deg_gene_exp = kirp_deg_gene_exp.loc[:, kirp_deg_gene_exp.loc['subtype'].str.contains("KIRP|normal")]