代码1:
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:
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))
上述代码1和代码2中df4.head()的输出是iPython中的一个正方形?我无法弄清楚原因。
输出:
df3的样子:
df的样子:
答案 0 :(得分:2)
您错过了一条退货声明:
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
返回None,因此空的DataFrame(由ipython呈现为正方形 - 这可能是一个错误)。
In [13]: df3.apply(lambda x: None)
Out[13]:
Empty DataFrame
Columns: []
Index: []
注意:在某些语言(例如Ruby,Julia,Scala)中,返回的最后一行没有显式返回。在Python中,如果你错过了return语句,函数将返回None。
In [21]: def foo():
1
In [22]: foo() == None
Out[22]: True