[英]Pandas replace value in multiindex row
So, I have a MultiIndex DataFrame and I cannot figure out row to modify a row index value.所以,我有一个 MultiIndex DataFrame 并且我无法找出行来修改行索引值。
In this example, I would like to set c = 1 where the "a" index is 4:在此示例中,我想设置 c = 1 其中“a”索引为 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:所需的 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.任何帮助表示赞赏。
Assuming we start with df
.假设我们从df
开始。
x = df.reset_index()
x.loc[x[x.a == 4].index, 'c'] = 1
x = x.set_index(['a', 'b', 'c'])
print(x)
colA
x1 x2 x3 x4
a b c
1 NaN 0 NaN NaN NaN NaN
4 NaN 1 NaN NaN NaN NaN
Separate the index, process it and put it back together with the data.分离索引,对其进行处理,然后将其与数据一起放回原处。
Method-1
将数据和 MultiIndex 结合在一起Method-1
Method-2
更新原dataframe Method-2
的索引# separate the index and process it
names = ['a', 'b', 'c'] # same as df.index.names
#dfd = pd.DataFrame(df.to_records())
dfd = df.index.to_frame().reset_index(drop=True)
dfd.loc[dfd['a']==4, ['c']] = 1
# prepare index for original dataframe: df
index = pd.MultiIndex.from_tuples([tuple(x) for x in dfd.loc[:, names].values], names=names)
## Method-1
# create new datframe with updated index
dfn = pd.DataFrame(df.values, index=index, columns=df.columns)
# dfn --> new dataframe
## Method-2
# reset the index of original dataframe df
df.set_index(index)
Output : Output :
colA
x1 x2 x3 x4
a b c
1.0 NaN 0.0 NaN NaN NaN NaN
4.0 NaN 1.0 NaN NaN NaN NaN
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.