Home > Back-end >  How do I obtain the common column names shared between multiple (more than two) Pandas dataframes?
How do I obtain the common column names shared between multiple (more than two) Pandas dataframes?

Time:01-15

How do I obtain the common column names shared between multiple (more than two) Pandas dataframes?

I tried np.intersect1d:

np.intersect1d(meth_kirp.columns, mrna_kirp.columns, cna_kirp.columns)

Traceback:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/tmp/ipykernel_27/1139729657.py in <module>
----> 1 np.intersect1d(meth_kirp.columns, mrna_kirp.columns, cna_kirp.columns)

<__array_function__ internals> in intersect1d(*args, **kwargs)

/opt/conda/lib/python3.7/site-packages/numpy/lib/arraysetops.py in intersect1d(ar1, ar2, assume_unique, return_indices)
    433     ar2 = np.asanyarray(ar2)
    434 
--> 435     if not assume_unique:
    436         if return_indices:
    437             ar1, ind1 = unique(ar1, return_index=True)

/opt/conda/lib/python3.7/site-packages/pandas/core/indexes/base.py in __nonzero__(self)
   2808     def __nonzero__(self):
   2809         raise ValueError(
-> 2810             f"The truth value of a {type(self).__name__} is ambiguous. "
   2811             "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
   2812         )

ValueError: The truth value of a Index is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

CodePudding user response:

You may try intersection using set:

set.intersection(set(meth_kirp.columns), set(mrna_kirp.columns), set(cna_kirp.columns))
  • Related