将具有多个参数的函数应用于滚动DataFrame Pandas

时间:2020-01-28 18:44:04

标签: python pandas

我有一个类似于下面的Pandas DataFrame。请注意,此格式不必一定是这种格式,它可以重新设置为透视样式,如果方便的话,可以将每个 ERROR: Command errored out with exit status 1: command: 'c:\users\r0504\appdata\local\programs\python\python37\python.exe' 'c:\users\r0504\appdata\local\programs\python\python37\lib\site-packages\pip' install --ignore-installed --no-user --prefix 'C:\Users\r0504\AppData\Local\Temp\pip-build-env-vv312rqa\overlay' --no-warn-script-location --no-binary :none: --only-binary :none: -i https://pypi.org/simple -- setuptools wheel pycairo cwd: None Complete output (33 lines): Collecting setuptools Using cached setuptools-45.1.0-py3-none-any.whl (583 kB) Collecting wheel Using cached wheel-0.34.1-py2.py3-none-any.whl (26 kB) Collecting pycairo Using cached pycairo-1.19.0.tar.gz (200 kB) Installing collected packages: setuptools, wheel, pycairo Running setup.py install for pycairo: started Running setup.py install for pycairo: finished with status 'error' ERROR: Command errored out with exit status 1: command: 'c:\users\r0504\appdata\local\programs\python\python37\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\r0504\\AppData\\Local\\Temp\\pip-install-irw93yia\\pycairo\\setup.py'"'"'; __file__='"'"'C:\\Users\\r0504\\AppData\\Local\\Temp\\pip-install-irw93yia\\pycairo\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\r0504\AppData\Local\Temp\pip-record-0_k2qosq\install-record.txt' --single-version-externally-managed --prefix 'C:\Users\r0504\AppData\Local\Temp\pip-build-env-vv312rqa\overlay' --compile --install-headers 'C:\Users\r0504\AppData\Local\Temp\pip-build-env-vv312rqa\overlay\Include\pycairo' cwd: C:\Users\r0504\AppData\Local\Temp\pip-install-irw93yia\pycairo\ Complete output (18 lines): running install running build running build_py creating build creating build\lib.win-amd64-3.7 creating build\lib.win-amd64-3.7\cairo copying cairo\__init__.py -> build\lib.win-amd64-3.7\cairo copying cairo\__init__.pyi -> build\lib.win-amd64-3.7\cairo copying cairo\py.typed -> build\lib.win-amd64-3.7\cairo running build_ext building 'cairo._cairo' extension creating build\temp.win-amd64-3.7 creating build\temp.win-amd64-3.7\Release creating build\temp.win-amd64-3.7\Release\cairo C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -DPYCAIRO_VERSION_MAJOR=1 -DPYCAIRO_VERSION_MINOR=19 -DPYCAIRO_VERSION_MICRO=0 -Ic:\users\r0504\appdata\local\programs\python\python37\include -Ic:\users\r0504\appdata\local\programs\python\python37\include "-IC:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\ATLMFC\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\cppwinrt" /Tccairo/device.c /Fobuild\temp.win-amd64-3.7\Release\cairo/device.obj device.c C:\Users\r0504\AppData\Local\Temp\pip-install-irw93yia\pycairo\cairo\pycairo.h(37): fatal error C1083: Cannot open include file: 'cairo.h': No such file or directory error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.24.28314\\bin\\HostX86\\x64\\cl.exe' failed with exit status 2 ---------------------------------------- ERROR: Command errored out with exit status 1: 'c:\users\r0504\appdata\local\programs\python\python37\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\r0504\\AppData\\Local\\Temp\\pip-install-irw93yia\\pycairo\\setup.py'"'"'; __file__='"'"'C:\\Users\\r0504\\AppData\\Local\\Temp\\pip-install-irw93yia\\pycairo\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\r0504\AppData\Local\Temp\pip-record-0_k2qosq\install-record.txt' --single-version-externally-managed --prefix 'C:\Users\r0504\AppData\Local\Temp\pip-build-env-vv312rqa\overlay' --compile --install-headers 'C:\Users\r0504\AppData\Local\Temp\pip-build-env-vv312rqa\overlay\Include\pycairo' Check the logs for full command output. ---------------------------------------- ERROR: Command errored out with exit status 1: 'c:\users\r0504\appdata\local\programs\python\python37\python.exe' 'c:\users\r0504\appdata\local\programs\python\python37\lib\site-packages\pip' install --ignore-installed --no-user --prefix 'C:\Users\r0504\AppData\Local\Temp\pip-build-env-vv312rqa\overlay' --no-warn-script-location --no-binary :none: --only-binary :none: -i https://pypi.org/simple -- setuptools wheel pycairo Check the logs for full command output. 列为一列。

variable

对于 date variable value 0 2014-01-31 item1 0.070898 1 2014-02-28 item1 0.064725 2 2014-03-31 item1 0.101292 3 2014-04-30 item1 0.041898 4 2014-05-31 item1 0.094894 5 2014-06-30 item1 0.110287 6 2014-07-31 item1 0.081844 7 2014-08-31 item1 0.043819 8 2014-09-30 item1 0.009196 9 2014-10-31 item1 0.076482 10 2014-11-30 item1 0.055906 11 2014-12-31 item1 0.042028 12 2015-01-31 item1 0.115469 13 2015-02-28 item1 0.130457 14 2015-03-31 item1 0.054314 15 2015-04-30 item1 0.141286 16 2015-05-31 item1 0.109387 17 2015-06-30 item1 0.066154 18 2015-07-31 item1 0.039390 19 2015-08-31 item1 0.071276 20 2015-09-30 item1 0.088784 21 2015-10-31 item1 0.054234 22 2015-11-30 item1 0.111854 23 2015-12-31 item1 0.005053 24 2016-01-31 item1 0.145953 25 2016-02-29 item1 0.138051 26 2016-03-31 item1 0.030395 27 2016-04-30 item1 0.055843 28 2016-05-31 item1 0.037960 29 2016-06-30 item1 0.147318 .. ... ... ... 120 2015-09-30 ref 0.043185 121 2015-10-31 ref 0.046849 122 2015-11-30 ref 0.008139 123 2015-12-31 ref 0.011222 124 2016-01-31 ref 0.026408 125 2016-02-29 ref 0.040404 126 2016-03-31 ref 0.039585 127 2016-04-30 ref 0.028782 128 2016-05-31 ref 0.002799 129 2016-06-30 ref 0.040413 130 2016-07-31 ref 0.004451 131 2016-08-31 ref 0.001946 132 2016-09-30 ref 0.029548 133 2016-10-31 ref 0.029416 134 2016-11-30 ref 0.024893 135 2016-12-31 ref 0.037656 136 2017-01-31 ref 0.045642 137 2017-02-28 ref 0.046690 138 2017-03-31 ref 0.019193 139 2017-04-30 ref 0.016362 140 2017-05-31 ref 0.025512 141 2017-06-30 ref 0.021625 142 2017-07-31 ref 0.026472 143 2017-08-31 ref 0.016531 144 2017-09-30 ref 0.047447 145 2017-10-31 ref 0.032180 146 2017-11-30 ref 0.048635 147 2017-12-31 ref 0.040450 148 2018-01-31 ref 0.047735 149 2018-02-28 ref 0.013727 中的每个项目,我想应用一个12个月的滚动计算,该计算接受该项目的variable的滚动12个月和值{的滚动12个月作为参数1}}等于value。该函数的形式为:

variable

我的问题是如何将两个不同的序列传递给此函数以返回单个值。有谁知道该怎么做?

1 个答案:

答案 0 :(得分:1)

关于您提到的功能,在用join()重构原始数据帧之后,这可能更接近您想要的功能:

combined = df[df['variable']!='ref'].set_index('date').join(df[df['variable']=='ref'].set_index('date'), lsuffix='', rsuffix='_ref').drop('variable_ref', axis=1)

def func(series, ref_series):

    #As an example
    return series.mean()/ref_series.mean()

combined.groupby('variable').rolling(12).apply(lambda x: func(x, combined.loc[x.index]['value_ref']), raw=False).drop('value_ref', axis=1)

此示例将产生以下结果(由于示例数据中存在空白,NaN出现了):

                        value
variable date                
item1    2014-01-31       NaN
         2014-02-28       NaN
         2014-03-31       NaN
         2014-04-30       NaN
         2014-05-31       NaN
         2014-06-30       NaN
         2014-07-31       NaN
         2014-08-31       NaN
         2014-09-30       NaN
         2014-10-31       NaN
         2014-11-30       NaN
         2014-12-31       NaN
         2015-01-31       NaN
         2015-02-28       NaN
         2015-03-31       NaN
         2015-04-30       NaN
         2015-05-31       NaN
         2015-06-30       NaN
         2015-07-31       NaN
         2015-08-31       NaN
         2015-09-30  1.912186
         2015-10-31  1.793184
         2015-11-30  2.609254
         2015-12-31  3.009455
         2016-01-31  3.123833
         2016-02-29  2.910599
         2016-03-31  2.708132
         2016-04-30  2.497878
         2016-05-31  2.561760
         2016-06-30  2.681712