代码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的外观:
您缺少返回语句:
如果没有返回
function
则返回None,因此是空数据帧(ipython将其呈现为正方形,这可能是一个bug)。你知道吗注意:在某些语言中(例如Ruby、Julia、Scala),最后一行返回时没有显式返回。在Python中,如果错过return语句,函数将返回None。你知道吗
相关问题 更多 >
编程相关推荐