2015-10-19 77 views
1

代码1:如何使用pandas通过数据框将连接函数应用于组?

df = pd.read_csv("example.csv", parse_dates=['d']) 
df2 = df.set_index(['d', 'c']) 
df3 = df2.groupby(level=['c']) 

def function(x): 
    a = pd.rolling_mean(x, 3).rename(columns = {'b':'rm'}) 
    c = pd.rolling_std(x, 3).rename(columns = {'b':'rsd'}) 
    pd.concat([x, a, c], axis=1) 

df4 = df3.apply(lambda x: function(x)) 

代码2:在上述两种代码1和代码2的

df = pd.read_csv("example.csv", parse_dates=['d']) 
df2 = df.set_index(['d', 'c']) 
df3 = df2.groupby(level=['c']) 

def function(x): 
    x.assign(rm = lambda x: pd.rolling_mean(x, 3)) 

df4 = df3.apply(lambda x: function(x)) 

输出df4.head的()是在IPython的正方形??我无法弄清楚为什么。

输出:

enter image description here

DF3什么样子:

enter image description here

看起来什么样DF:

enter image description here

+0

你可以试试 - ''x = pd.concat([x,a,c],axis = 1)'? –

+0

是的,我确实尝试过。同样的错误! – pr338

+0

什么错误?你使用'df.plot()'来得到那个方块吗?你能展示你的数据框的例子吗? –

回答

2

你错过A R E打开声明:

In [11]: def function(x): 
      a = pd.rolling_mean(x, 3).rename(columns = {'bookings':'rm'}) 
      c = pd.rolling_std(x, 3).rename(columns = {'bookings':'rsd'}) 
      return pd.concat([x, a, c], axis=1) 

In [12]: df3.apply(lambda x: function(x)) 
Out[12]: 
        bookings   rm  rsd 
ds   city 
2013-01-01 City_2  69   NaN  NaN 
2013-01-02 City_2  101   NaN  NaN 
2013-01-03 City_2  134 101.333333 32.501282 
2013-01-04 City_2  155 130.000000 27.221315 
2013-01-05 City_2  104 131.000000 25.632011 
2013-01-06 City_2  121 126.666667 25.967929 
2013-01-07 City_2  143 122.666667 19.553346 
2013-01-08 City_2  173 145.666667 26.102363 
2013-01-09 City_2  142 152.666667 17.616280 
2013-01-10 City_2  154 156.333333 15.631165 
2013-01-11 City_2  139 145.000000 7.937254 

没有回报function返航无,因此空数据框(这是由IPython中呈现为一个正方形 - 这可能是一个错误)。

In [13]: df3.apply(lambda x: None) 
Out[13]: 
Empty DataFrame 
Columns: [] 
Index: [] 

注:在某些语言(如红宝石,朱莉娅,斯卡拉)返回的最后一行没有被明确而归。在Python中,如果你错过了返回语句,该函数返回None。

In [21]: def foo(): 
      1 

In [22]: foo() == None 
Out[22]: True 
相关问题