python - Pandas resample to return just one column after an apply as been made -
def wk(args): return args['open'].mean() - args['close'].mean() df = pd.dataframe() df = data.resample("2b").apply(wk)
i run following code on below dataframe:
open high low close volume date 2016-01-04 860.0 868.0 849.0 856.0 314041.0 2016-01-05 867.5 870.0 844.0 853.5 292475.0 2016-01-06 863.0 863.0 844.0 861.0 312689.0 2016-01-07 872.0 901.0 871.5 899.5 870578.0
which returns:
open high low close volume date 2016-01-04 9.00 9.00 9.00 9.00 9.00 2016-01-06 -12.75 -12.75 -12.75 -12.75 -12.75
it's dubious have fives columns same data. how can make resample , apply return 1 column?
so may write
df['one column'] = data.resample("2b").apply(wk)
instead of
df = data.resample("2b").apply(wk)
row-wise apply , resampler dispatch
use row-wise .apply(func, axis=1)
, turn resampled object (returned pandas.tseries.resample.datetimeindexresampler
) dataframe dispatch method (sum, mean, first, last...):
import pandas pd data = pd.read_csv(stringio('''date,open,high,low,close,volume 2016-01-04,860.0,868.0,849.0,856.0,314041.0 2016-01-05,867.5,870.0,844.0,853.5,292475.0 2016-01-06,863.0,863.0,844.0,861.0,312689.0 2016-01-07,872.0,901.0,871.5,899.5,870578.0'''), index_col=0, parse_dates=true) def wk(args): return args['open'].mean() - args['close'].mean() df = data.resample('2b').mean().apply(wk, axis=1) print df
date 2016-01-04 9.00 2016-01-06 -12.75 freq: 2b, dtype: float64
Comments
Post a Comment