Pass Entire Contents Of A Dataframe To A Function In Pandas
I'm trying to rework much of my analysis code for signal processing using Dataframes instead of numpy arrays. However, I'm having a hard time figuring out how to pass the entire m
Solution 1:
You can get the raw numpy array version of a DataFrame with df.values
. However, in many cases you can just pass the DataFrame itself, since it still allows use of the normal numpy API (i.e., it has all the right methods).
Solution 2:
http://pandas.pydata.org/pandas-docs/dev/generated/pandas.DataFrame.apply.html this will allow you to perform operations on a row (or column, or the entire dataframe).
import random
signal=pd.DataFrame([[10*random.random() for _ inrange(3)]for _ inrange(5)])
deftestm(frame, average=0):
return frame-average
signal.apply(testm,average=signal.mean(),axis=1)
results:
signal
Out[57]:
01205.5664457.6120708.55496610.8691582.3824296.19727225.9331923.5645279.80566939.6762921.7079442.73147945.3196293.3483376.476631
signal.mean()
Out[59]:
05.47294313.72306226.753203
dtype: float64
signal.apply(testm,average=signal.mean(),axis=1)
Out[58]:
01200.0935023.8890081.8017631 -4.603785 -1.340632 -0.55593220.460249 -0.1585343.05246634.203349 -2.015117 -4.0217244 -0.153314 -0.374724 -0.276572
This will take the mean of each column, and subtract it from each value in that column.
Post a Comment for "Pass Entire Contents Of A Dataframe To A Function In Pandas"