How do I change or access pandas MultiIndex column headers?

mankoff

I have the following Pandas DataFrame, but am having trouble updating a column header value, or easily accessing the header values (for example, for plotting a time at the (lon,lat) location from the header).

df = pd.DataFrame(columns = ["id0", "id1", "id2"])
df.loc[2012]= [24, 25, 26]
df.loc[2013]= [28, 28, 29]
df.loc[2014]= [30, 31, 32]

df.columns = pd.MultiIndex.from_arrays([df.columns, [66,67,68], [110,111,112]],
                                       names=['id','lat','lon'])

Which then looks like this:

>>> df
id     id0   id1   id2
lat     66    67    68
lon    110   111   112
2012  24.0  25.0  26.0
2013  28.0  28.0  29.0
2014  30.0  31.0  32.0

I'd like to be able to adjust the latitude or longitude for df['id0'], or plot(df.ix[2014]) but at (x,y) location based on (lon,lat).

hilberts_drinking_problem

You can use df.columns.get_level_values('lat') in order to get the index object. This returns a copy of the index, so you cannot extend this approach to modify the coordinates inplace.

However, you can access the levels directly and modify them inplace using this workaround.

import pandas as pd
import numpy as np

df = pd.DataFrame(columns = ["id0", "id1", "id2"])
df.loc[2012]= [24, 25, 26]
df.loc[2013]= [28, 28, 29]
df.loc[2014]= [30, 31, 32]

df.columns = pd.MultiIndex.from_arrays([df.columns, [66,67,68], [110,111,112]],
                                       names=['id','lat','lon'])

ids = df.columns.get_level_values('id')
id_ = 'id0'
column_position = np.where(ids.values == id_)

new_lat = 90
new_lon = 0

df.columns._levels[1].values[column_position] = new_lat
df.columns._levels[2].values[column_position] = new_lon

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I change order/grouping/level of pandas MultiIndex columns?

From Dev

pandas how do I retrieve the index column of multiIndex

From Dev

How to access MultiIndex column after groupby in pandas?

From Java

Creating a Pandas DataFrame from a Numpy array: How do I specify the index column and column headers?

From Dev

Pandas: convert a multiindex column headers into normal column header?

From Dev

pandas, how to access multiIndex dataframe?

From Dev

How to access multiindex levels in pandas?

From Dev

Change column values to column headers in pandas

From Dev

How to do group by on a multiindex in pandas?

From Java

How can I make pandas dataframe column headers all lowercase?

From Dev

How can I transform a pandas dataframe into a dictionary without the column headers?

From Dev

How do I replace the Column Headers in a String with a list of replacement Column Headers?

From Dev

How do I change file headers from the command line?

From Dev

How do I create a UICollectionView with column and row headers?

From Dev

How do I use values as a column headers in a SQL Server Query

From Dev

How do I change phpMyAdmin access URL?

From Dev

Setting DataFrame column headers to a MultiIndex

From Dev

How to get Pandas column multiindex names as a list

From Dev

How to check if a column exists in a pandas MultiIndex

From Dev

How do i set now() on column change?

From Dev

How do I change NA into Column median

From Dev

How do I create aggregates by a column in pandas?

From Dev

How do you shift Pandas DataFrame with a multiindex?

From Dev

I want to bypass the headers and have just the sum of the amount column, how do I do that?

From Dev

How do I convert a MultiIndex to type string

From Java

how do I insert a column at a specific column index in pandas?

From Dev

How do I get the change from the same quarter in the previous year in a pandas datatable grouped by more than 1 column

From Dev

How to access pandas multiindex by element within a nested dict?

From Dev

How do I access/change pixels in a javascript image object?

Related Related

  1. 1

    How do I change order/grouping/level of pandas MultiIndex columns?

  2. 2

    pandas how do I retrieve the index column of multiIndex

  3. 3

    How to access MultiIndex column after groupby in pandas?

  4. 4

    Creating a Pandas DataFrame from a Numpy array: How do I specify the index column and column headers?

  5. 5

    Pandas: convert a multiindex column headers into normal column header?

  6. 6

    pandas, how to access multiIndex dataframe?

  7. 7

    How to access multiindex levels in pandas?

  8. 8

    Change column values to column headers in pandas

  9. 9

    How to do group by on a multiindex in pandas?

  10. 10

    How can I make pandas dataframe column headers all lowercase?

  11. 11

    How can I transform a pandas dataframe into a dictionary without the column headers?

  12. 12

    How do I replace the Column Headers in a String with a list of replacement Column Headers?

  13. 13

    How do I change file headers from the command line?

  14. 14

    How do I create a UICollectionView with column and row headers?

  15. 15

    How do I use values as a column headers in a SQL Server Query

  16. 16

    How do I change phpMyAdmin access URL?

  17. 17

    Setting DataFrame column headers to a MultiIndex

  18. 18

    How to get Pandas column multiindex names as a list

  19. 19

    How to check if a column exists in a pandas MultiIndex

  20. 20

    How do i set now() on column change?

  21. 21

    How do I change NA into Column median

  22. 22

    How do I create aggregates by a column in pandas?

  23. 23

    How do you shift Pandas DataFrame with a multiindex?

  24. 24

    I want to bypass the headers and have just the sum of the amount column, how do I do that?

  25. 25

    How do I convert a MultiIndex to type string

  26. 26

    how do I insert a column at a specific column index in pandas?

  27. 27

    How do I get the change from the same quarter in the previous year in a pandas datatable grouped by more than 1 column

  28. 28

    How to access pandas multiindex by element within a nested dict?

  29. 29

    How do I access/change pixels in a javascript image object?

HotTag

Archive