python - manipulating multiindex columns in Pandas -
i have simple multiindex dataframe in pandas. i'm trying add additional subcolumns, i'm being warned off
settingwithcopywarning: value trying set on copy of slice dataframe. try using .loc[row_indexer,col_indexer] = value instead
i cannot manage right indexing incantation make work.
attaching code fragment has simple, non-hierarchical example of sorts of columns want add. have hierchical example demonstrate how can add new top-level colummns, cannot manipulate individual sub-columns
import pandas pd import numpy np #simple example works: add 2 columns non-hierarchical frame sdf = pd.dataframe(np.random.randn(6,4),columns=list('abcd')) sdf['e'] = 7 sdf['f'] = sdf['a'].diff(-1) #hierarchical example df = pd.dataframe({('co1', 'price'): {0: 1, 1: 2, 2:12, 3: 14, 4: 15},\ ('co1', 'size'): {0: 1, 1: 5, 2: 9, 3: 13, 4: 17},\ ('co2', 'price'): {0: 2, 1: 6, 2: 10, 3: 14, 4: 18},\ ('co2', 'size'): {0: 3, 1: 7, 2: 11, 3: 15, 4: 19}}) df.index.names = ['run'] df.columns.names = ['security', 'characteristic'] #i can add new top level column df['newtoplevel?'] = "yes" #i cannot manipulate values of existing sub-level columns """a value trying set on copy of slice dataframe. try using .loc[row_indexer,col_indexer] = value instead""" df['co1']['size'] = "gross" df['co1']['price'] = df['co1']['price']*2 #i cannot add new sub-level column df['co1']['new_sub_col'] = "fails"
i seem missing fundamental understanding of issue, frustrating i've read pretty closely o'reilly "python data analysis" book written pandas author! ugh.
to avoid warning/error use loc , these in 1 assignment:
in [11]: df.loc[:, ('co1', 'size')] = "gross" in [12]: df.loc[:, ('co1', 'price')] *= 2 in [13]: df.loc[:, ('co1', 'new_sub_col')] = "fails" # not anymore in [14]: df out[14]: security co1 co2 newtoplevel? co1 characteristic price size price size new_sub_col run 0 2 gross 2 3 yes fails 1 4 gross 6 7 yes fails 2 24 gross 10 11 yes fails 3 28 gross 14 15 yes fails 4 30 gross 18 19 yes fails
Comments
Post a Comment