按索引级别为Pandas Multiindex DataFrame分配值

Art*_*örk 6 python multi-index pandas

我有一个Pandas multiindex数据帧,我需要为一个系列中的一个列赋值.该系列与数据帧索引的第一级共享其索引.

import pandas as pd
import numpy as np
idx0 = np.array(['bar', 'bar', 'bar', 'baz', 'foo', 'foo'])
idx1 = np.array(['one', 'two', 'three', 'one', 'one', 'two'])
df = pd.DataFrame(index = [idx0, idx1], columns = ['A', 'B'])
s = pd.Series([True, False, True],index = np.unique(idx0))
print df
print s
Run Code Online (Sandbox Code Playgroud)

出:

             A    B
bar one    NaN  NaN
    two    NaN  NaN
    three  NaN  NaN
baz one    NaN  NaN
foo one    NaN  NaN
    two    NaN  NaN

bar     True
baz    False
foo     True
dtype: bool
Run Code Online (Sandbox Code Playgroud)

这些不起作用:

df.A = s # does not raise an error, but does nothing
df.loc[s.index,'A'] = s # raises an error
Run Code Online (Sandbox Code Playgroud)

预期产量:

             A     B
bar one    True   NaN
    two    True   NaN
    three  True   NaN
baz one    False  NaN
foo one    True   NaN
    two    True   NaN
Run Code Online (Sandbox Code Playgroud)

Joh*_*hnE 6

系列(和字典)可以像map和apply一样使用函数(感谢@normanius改进语法):

df['A'] = pd.Series(df.index.get_level_values(0)).map(s).values
Run Code Online (Sandbox Code Playgroud)

或类似地:

df['A'] = df.reset_index(level=0)['level_0'].map(s).values
Run Code Online (Sandbox Code Playgroud)

结果:

A    B
bar one     True  NaN
    two     True  NaN
    three   True  NaN
baz one    False  NaN
foo one     True  NaN
    two     True  NaN
Run Code Online (Sandbox Code Playgroud)

  • @JohnE:我建议写`df ['A'] = pd.Series(df.index.get_level_values(0)).map(s).values`,这比你的例子更健壮,更清晰. (2认同)