How to check if a column exists in a pandas MultiIndex

LateCoder

Let's say I have a DataFrame with a MultiIndex of columns like this:

In [29]: df = pd.DataFrame([[0] * 8], columns = pd.MultiIndex.from_product(
    [['a', 'b'], [1, 2], [2000, 2001]])
)

In [30]: df
Out[30]:
     a                   b
     1         2         1         2
  2000 2001 2000 2001 2000 2001 2000 2001
0    0    0    0    0    0    0    0    0

In [46]: df.columns.levels
Out[46]: FrozenList([[u'a', u'b'], [1, 2], [2000, 2001]])

I need to know, for all values of level 0 and some specific value of level 1, what are all the existing unique values of level 2 (say the DataFrame goes through some process in which for some values of level 1 and level 0, level 2 is dropped). The best I've been able to come up with so far is this:

In [54]: level_1_val = 2

In [55]: cols_series = df.columns.to_series()

In [56]: cols_series[
   ....:     cols_series.index.get_level_values(1) == level_1_val
   ....: ].index.get_level_values(2).unique()

array([2000, 2001])

What's a better way to do this?

piRSquared

IIUC

df.xs(2, axis=1, level=1).groupby(axis=1, level=1).first().columns.values

array([2000, 2001])

Or

df.xs(2, axis=1, level=1).columns.get_level_values(level=1).unique()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to check if a column exists in Pandas

From Dev

Pandas - How to check if multi index column exists

From Java

How to check if a column exists in a datatable

From Dev

How to check if column exists in the Datarow

From Dev

python pandas - check if a string type exists in a column

From Dev

How to get Pandas column multiindex names as a list

From Dev

How to access MultiIndex column after groupby in pandas?

From Dev

python pandas - Check if partial string in column exists in other column

From Dev

python pandas - Check if partial string in column exists in other column

From Java

How to check if a column exists in a SQL Server table?

From Dev

How to check if a column exists in a matrix or data frame?

From Dev

How to check if a column exists in Impala table?

From Dev

How to check if column exists in sqlite in Qt

From Dev

How to check if Username exists in specific column of database

From Java

Pandas DataFrame check if column value exists in a group of columns

From Dev

Pandas DataFrame check if column value exists in a group of columns

From Dev

How do I change or access pandas MultiIndex column headers?

From Dev

How to create a column depending on the row index values in a multiindex pandas dataframe?

From Dev

How to move pandas data frame multiindex column into 2 rows

From Dev

pandas how do I retrieve the index column of multiIndex

From Dev

How to create a column depending on the row index values in a multiindex pandas dataframe?

From Dev

How to get Pandas column multiindex FULL name as a list

From Dev

Check if a column exists in SQLite

From Dev

Pytables check if column exists

From Dev

Check if a column exists in mysql

From Dev

Check if column exists in linq

From Dev

Pytables check if column exists

From Dev

Idiomatic multiindex column assignment in pandas

From Dev

Remove column from Pandas multiindex

Related Related

  1. 1

    How to check if a column exists in Pandas

  2. 2

    Pandas - How to check if multi index column exists

  3. 3

    How to check if a column exists in a datatable

  4. 4

    How to check if column exists in the Datarow

  5. 5

    python pandas - check if a string type exists in a column

  6. 6

    How to get Pandas column multiindex names as a list

  7. 7

    How to access MultiIndex column after groupby in pandas?

  8. 8

    python pandas - Check if partial string in column exists in other column

  9. 9

    python pandas - Check if partial string in column exists in other column

  10. 10

    How to check if a column exists in a SQL Server table?

  11. 11

    How to check if a column exists in a matrix or data frame?

  12. 12

    How to check if a column exists in Impala table?

  13. 13

    How to check if column exists in sqlite in Qt

  14. 14

    How to check if Username exists in specific column of database

  15. 15

    Pandas DataFrame check if column value exists in a group of columns

  16. 16

    Pandas DataFrame check if column value exists in a group of columns

  17. 17

    How do I change or access pandas MultiIndex column headers?

  18. 18

    How to create a column depending on the row index values in a multiindex pandas dataframe?

  19. 19

    How to move pandas data frame multiindex column into 2 rows

  20. 20

    pandas how do I retrieve the index column of multiIndex

  21. 21

    How to create a column depending on the row index values in a multiindex pandas dataframe?

  22. 22

    How to get Pandas column multiindex FULL name as a list

  23. 23

    Check if a column exists in SQLite

  24. 24

    Pytables check if column exists

  25. 25

    Check if a column exists in mysql

  26. 26

    Check if column exists in linq

  27. 27

    Pytables check if column exists

  28. 28

    Idiomatic multiindex column assignment in pandas

  29. 29

    Remove column from Pandas multiindex

HotTag

Archive