How can I find the dtype
of a MultiIndex
in pandas without printing out all the values (this happens if you type df.index
, for example). I just want to know the names of the indices and their types.
Ex:
df = pd.DataFrame({"a": np.arange(100000), "b": np.arange(100000)[::-1]}, index=pd.MultiIndex.from_arrays([np.arange(100000), np.arange(100000)[::-1]], names=["i1", "i2"]))
doing:
df.index
will take some time to actually print, for a large df that's too long.
The MultiIndex object is the hierarchical analogue of the standard Index object which typically stores the axis labels in pandas objects. You can think of MultiIndex as an array of tuples where each tuple is unique. A MultiIndex can be created from a list of arrays (using MultiIndex.
from_tuples() function is used to convert list of tuples to MultiIndex. It is one of the several ways in which we construct a MultiIndex.
Use get_level_values:
df.index.get_level_values(0).dtype
dtype('int64')
and
df.index.get_level_values(1).dtype
dtype('int64')
For names use:
df.index.names
FrozenList(['i1', 'i2'])
Coming soon to a distro near you, you MultiIndex.dtypes
will soon be available:
df.index.dtypes
i1 int64
i2 int64
dtype: object
For older versions, if your index is not too large, you can first convert to a frame using to_frame
, then query the dtypes:
df.index.to_frame().dtypes
i1 int64
i2 int64
dtype: object
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With