将函数应用于可返回多行的pandas DataFrame [英] Apply function to pandas DataFrame that can return multiple rows

查看:113
本文介绍了将函数应用于可返回多行的pandas DataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试转换DataFrame,以便将某些行复制给定的次数.例如:

I am trying to transform DataFrame, such that some of the rows will be replicated a given number of times. For example:

df = pd.DataFrame({'class': ['A', 'B', 'C'], 'count':[1,0,2]})

  class  count
0     A      1
1     B      0
2     C      2

应转换为:

  class 
0     A   
1     C   
2     C 

这与带计数功能的聚合相反.有没有一种简单的方法可以在熊猫中实现它(不使用for循环或列表推导)?

This is the reverse of aggregation with count function. Is there an easy way to achieve it in pandas (without using for loops or list comprehensions)?

一种可能是允许DataFrame.applymap函数返回多行(类似于GroupByapply方法).但是,我认为现在在大熊猫中是不可能的.

One possibility might be to allow DataFrame.applymap function return multiple rows (akin apply method of GroupBy). However, I do not think it is possible in pandas now.

推荐答案

您可以使用groupby:

You could use groupby:

def f(group):
    row = group.irow(0)
    return DataFrame({'class': [row['class']] * row['count']})
df.groupby('class', group_keys=False).apply(f)

所以你得到

In [25]: df.groupby('class', group_keys=False).apply(f)
Out[25]: 
  class
0     A
0     C
1     C

您可以根据需要固定结果的索引

You can fix the index of the result however you like

这篇关于将函数应用于可返回多行的pandas DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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