假设我在索引和列上都有多个索引的DataFrame。
first x y
second m n
A B
A0 B0 0 0
B1 0 0
A1 B0 0 0
B1 0 0
我试图用条件更新值。条件类似于: `规则:
[{condition:{'A':'A0,'B':'B0'},value:5},
{condition:{'B':'B1'},value:3},
.....]
我试图找到与
具有相似功能的内容使用pandas.DataFrame.xs
进行设置值:
for each rule in rules:
df.xs((conditions.values), level=[conditions.keys]) = value
将多个级别传递给pandas.Index.get_level_values
以设置值:
for each rule in rules:
df.loc[df.index.get_level_values(conditions.keys) == [conditions.values] = value
结果应为
first x y
second m n
A B
A0 B0 5 5
B1 3 3
A1 B0 0 0
B1 3 3`