Python 由于联接或级别混淆,多索引列上的元素操作失败
我有一个三级多指标列Python 由于联接或级别混淆,多索引列上的元素操作失败,python,pandas,Python,Pandas,我有一个三级多指标列名称、周期、测量值在我的玩具示例中,我有三个不同的人在“之前”和“之后”两个时间段的体重和身高 我想生成一个数据框,显示之前期间三个名字的重量除以高度 我在使用我怀疑的level=参数时犯了某种错误(或者可能。但是无论我怎么做,我都会得到一个TypeError:Join on level在两个多索引对象之间是不明确的 我想了解如何控制操作,以便我可以指定“按姓名显示,其中period='before',体重除以身高”以及更一般的“按姓名显示,按时段显示体重除以身高” 您就快到
名称、周期、测量值
在我的玩具示例中,我有三个不同的人在“之前”和“之后”两个时间段的体重和身高
我想生成一个数据框,显示之前期间三个名字的重量除以高度
我在使用我怀疑的level=
参数时犯了某种错误(或者可能。但是无论我怎么做,我都会得到一个TypeError:Join on level在两个多索引对象之间是不明确的
我想了解如何控制操作,以便我可以指定“按姓名显示,其中period='before',体重除以身高”以及更一般的“按姓名显示,按时段显示体重除以身高”
您就快到了。在除法过程中错过了值
:
print(xf.loc[:, idx[:,'before','weight']].divide(xf.loc[:, idx[:,'before','height']].values, level=[0], axis='columns'))
结果:
name mike matt dave
period before before before
statistic weight weight weight
2019-12-01 111.111111 111.111111 111.111111
2019-12-02 105.555556 105.555556 105.555556
为了正确对齐,必须使用相同的多索引值,因此您可以在此处使用重命名
:
print (xf.loc[:, idx[:,'before','weight']]
.divide(xf.loc[:, idx[:,'before','height']].rename(columns={'height':'weight'})))
name mike matt dave
period before before before
statistic weight weight weight
2019-12-01 111.111111 111.111111 111.111111
2019-12-02 105.555556 105.555556 105.555556
如果要将输出附加到原始数据帧
:
idx = pd.IndexSlice
df = (xf.loc[:, idx[:,'before','weight']].rename(columns={'weight':'new'})
.divide(xf.loc[:, idx[:,'before','height']].rename(columns={'height':'new'})))
xf = pd.concat([xf, df], axis=1).sort_index(axis=1)
print (xf)
name dave matt \
period after before after before
statistic height weight height new weight height weight height
2019-12-01 1.7 170 1.8 111.111111 200 1.7 170 1.8
2019-12-02 1.7 166 1.8 105.555556 190 1.7 166 1.8
name mike
period after before
statistic new weight height weight height new weight
2019-12-01 111.111111 200 1.7 170 1.8 111.111111 200
2019-12-02 105.555556 190 1.7 166 1.8 105.555556 190
如果可能,您可以使用,因此解决方案是简化:
df1 = xf.xs(('before','weight'), axis=1, level=[1,2])
df2 = xf.xs(('before','height'), axis=1, level=[1,2])
print (df1.div(df2))
name mike matt dave
2019-12-01 111.111111 111.111111 111.111111
2019-12-02 105.555556 105.555556 105.555556
有点随机,为什么我会收到这样的消息:NameError:name'idx'未定义
@SathishSanjeevi抱歉,忘记了定义。idx=pd.indexlice
我将其添加回示例中。
df1 = xf.xs(('before','weight'), axis=1, level=[1,2])
df2 = xf.xs(('before','height'), axis=1, level=[1,2])
print (df1.div(df2))
name mike matt dave
2019-12-01 111.111111 111.111111 111.111111
2019-12-02 105.555556 105.555556 105.555556