So, I have a MultiIndex DataFrame and I cannot figure out row to modify a row index value.
In this example, I would like to set c = 1 where the "a" index is 4:
import pandas as pd
import numpy as np
df = pd.DataFrame({('colA', 'x1'): {(1, np.nan, 0): np.nan, (4, np.nan, 0): np.nan},
('colA', 'x2'): {(1, np.nan, 0): np.nan, (4, np.nan, 0): np.nan},
('colA', 'x3'): {(1, np.nan, 0): np.nan, (4, np.nan, 0): np.nan},
('colA', 'x4'): {(1, np.nan, 0): np.nan, (4, np.nan, 0): np.nan}})
df.index.set_names(['a', 'b', 'c'], inplace=True)
print(df)
colA
x1 x2 x3 x4
a b c
1 NaN 0 NaN NaN NaN NaN
4 NaN 0 NaN NaN NaN NaN
Desired output:
colA
x1 x2 x3 x4
a b c
1 NaN 0 NaN NaN NaN NaN
4 NaN 1 NaN NaN NaN NaN
Any help is appreciated.
pandas[df.index['a'] == 4] = 1? maybe/df[df.index['a'] == 4] = 1\IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indicesmask = df.index.get_level_values('a') == 4