为什么在Pandas中使用Apply时会有额外的索引 [英] Why there is an extra index when using apply in Pandas

查看:104
本文介绍了为什么在Pandas中使用Apply时会有额外的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在熊猫中的用户定义函数上使用apply时,似乎python正在创建其他数组.我该如何摆脱呢?这是我的代码:

When I use apply to a user defined function in Pandas, it looks like python is creating an additional array. How could I get rid of it? Here is my code:

def fnc(group):
    x = group.C.values
    out = x[np.where(x < 0)]
    return pd.DataFrame(out)

data = pd.DataFrame({'A':np.random.randint(1, 3, 10),
                     'B':3,
                     'C':np.random.normal(0, 1, 10)})

data.groupby(by=['A', 'B']).apply(fnc).reset_index()

创建了这个奇怪的Level_2索引.运行我的函数时,有什么方法可以避免创建它?

There is this weird Level_2 index created. Is there a way to avoid creating it when running my function?

    A   B   level_2   0
0   1   3   0        -1.054134802
1   1   3   1        -0.691996447
2   2   3   0        -1.068693768
3   2   3   1        -0.080342046
4   2   3   2        -0.181869799

推荐答案

这样,您将无法避免出现level_2.这是因为分组的结果是一个其中包含多个项目的数据框:pandas非常酷,可以理解您希望在这些分组的键之间广播这些项目,但是它将数据框的索引作为附加级别来确保相干的输出数据.因此,应该在处理结束时明确地将level = -1丢弃.

As such, you will have no way to avoid level_2 appearing. This is because the result of your grouping is a dataframe with several items in it: pandas is cool enough to understand your wish is to broadcast these items across the grouped keys, yet it is taking the index of the dataframe as an additional level to guarantee coherent output data. So dropping level=-1 at the end of your processing explicitly is expected.

如果要避免重设该额外索引,但仍要进行一些后期处理,另一种方法是调用transform而不是apply,并从fnc获取返回的数据,该数据是放置np.nan排除结果.然后,您的数据框将没有额外的级别,但是您随后需要调用dropna().

If you want to avoid to reset that extra index, but still have some post processing, another way would be to call transform instead of apply, and get the returned data from fnc being the entire group vector where you put np.nan for results to exclude. Then, your dataframe will not have an extra level, but you'll need to call dropna() afterwards.

这篇关于为什么在Pandas中使用Apply时会有额外的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆