如何使用pandas对分组数据帧应用concat函数?

2024-09-28 03:19:39 发布

您现在位置:Python中文网/ 问答频道 /正文

代码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中的一个正方形??我不明白为什么。你知道吗

输出:

enter image description here

df3的外观:

enter image description here

df的外观:

enter image description here


Tags: csvlambda代码dfreadparseexamplefunction
1条回答
网友
1楼 · 发布于 2024-09-28 03:19:39

您缺少返回语句:

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,因此是空数据帧(ipython将其呈现为正方形,这可能是一个bug)。你知道吗

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

相关问题 更多 >

    热门问题