简体   繁体   中英

set the value of multiindex in pandas dataframe

This is an old question which is raised again because of pandas' deprecation.

[https://stackoverflow.com/questions/23108889/set-value-multiindex-pandas]

The previous provided solution doesnt work anymore. below is what it does now as the second param within loc now means column and not the nth multiindex.

>>> ticks = pd.DataFrame(index = multindex, columns = ['change'])
>>> ticks.loc[(p,1)] = 1
>>> ticks
                          change    1
datetime            2nd                                   
NaT                 NaN      NaN  NaN
2020-12-01 01:10:01          NaN  1.0
>>> ticks.loc[(p,2)] = 1
>>> ticks
                          change  1    2
datetime            2nd                                        
NaT                 NaN      NaN  NaN  NaN
2020-12-01 01:10:01          NaN  1.0  1.0

What is the new solution? and why the hell is such a crucial information not founded on pandas documentation?

found the solution here an updated solution to "adding a row to a MultiIndex DataFrame/Series"
it is necessary to add the: to indicate all axes. ie

>>> ticks.loc[(p,2),:] = 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM