Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何有效地将函数应用于面板的每个数据帧_Python_Pandas - Fatal编程技术网

Python 如何有效地将函数应用于面板的每个数据帧

Python 如何有效地将函数应用于面板的每个数据帧,python,pandas,Python,Pandas,我试图将一个函数应用于Pandas面板中的每个数据帧。我可以把它写成循环,但是索引似乎需要很长时间。我希望内置的熊猫功能可能会更快 我的数据帧看起来像(实际上每列大约50行): 它们排列在带有多索引键的面板中: mydict = { (0, 20 ) : mydata, (30, 40 ) : moredata } mypanel = pd.Panel( mydict ) 面板如下所示: <class 'pandas.core.panel.Panel'> Dimensions: 1

我试图将一个函数应用于Pandas面板中的每个数据帧。我可以把它写成循环,但是索引似乎需要很长时间。我希望内置的熊猫功能可能会更快

我的数据帧看起来像(实际上每列大约50行):

它们排列在带有多索引键的面板中:

mydict = { (0, 20 ) : mydata, (30, 40 ) : moredata }
mypanel = pd.Panel( mydict )
面板如下所示:

<class 'pandas.core.panel.Panel'>
Dimensions: 1600 (items) x 48 (major_axis) x 2 (minor_axis)
Items axis: (-4000, -4000) to (3800, 3800)
Major_axis axis: 0 to 47
Minor_axis axis: hits to sqerr
我想将面板简化为一个系列,通过多重索引进行索引,并将冷凝器函数的结果作为其值

我可以做到:

intermediate = []
for k, df in mypanel.iteritems():
    intermediate.append( condenser( df ) )

result = pd.Series( results, index = pypanel.items )
这会给出所需的结果,但当我对其进行分析时,只有4%的时间花在我的
冷凝器
功能上。大部分时间都花在
iteritems
\uu getitem\uuuu
上,所以我想知道是否可以做得更好

我查看了
mypanel.apply(concerator,axis='items')
,但这会分别在我的数据帧的每一列上循环。有什么东西可以对每个数据帧应用一个函数吗


注意:我使用的是Python 2.7.9和pandas 0.15.2,apply是正确的,但是用法是:

mypanel.apply(冷凝器,轴=[1,2])


这将把一个48 x 2的数据帧传递到concerter中。

我必须将concerter的返回值显式地转换为float,但是它工作得很好。
def condenser( df ):
    return some_stuff( df['hits'], df['sqerr'] )
intermediate = []
for k, df in mypanel.iteritems():
    intermediate.append( condenser( df ) )

result = pd.Series( results, index = pypanel.items )