【发布时间】:2017-03-01 09:28:57
【问题描述】:
我想在 python / pandas 中使用方法,例如函数中的参数。 例如数据帧的滚动统计:
def rolling (df, prefix = 'r', window = 3, method = 'here I wanna choose a method' ):
for name in df.columns:
df[prefix + name] = df[name].rolling(window).'here this method been called'
return df
'mean()' 或 'sum()' 或其他... 喜欢
df.rolling(2).sum()
我在 R 中工作了 95% 的时间,在 R 中这很简单(将函数作为参数或返回任何函数)。但是在python中我是菜鸟。所以我创建了一个包来让我更轻松。喜欢:
def head(x,k = 3):
return x.head(k)
python 中的什么函数可以帮助我在函数中使用类似参数的方法?
#some data
import numpy as np
import pandas as pd
from pandas_datareader.data import DataReader
from datetime import datetime
ibm = DataReader('IBM', 'yahoo', datetime(2000,1,1), datetime(2016,1,1))
ibm2 = rolling(ibm,'rr', 5, 'sum') # something like this
【问题讨论】:
标签: python r function pandas methods