Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
column wise median

Hi, this is a very nice platform, iam new to here

i have difficult to compute the columns wise median:
i have serval factors: factor1, factor2, factor3, factor4.....
how can i calculate the median of these factors?

i tried :

np.median(pd.concat([factor1, factor2, factor3, factor4....]),axis=0)
TypeError: cannot concatenate a non-NDFrame object

np.median(pd.DataFrame([factor1, factor2, factor3, factor4....]),axis=0)
TypeError: zipline.pipeline.pipeline.validate_column() expected a value of type zipline.pipeline.term.Term for argument 'term', but got numpy.ndarray instead.

Any help from you will be greatly appreciated

5 responses

Hi ce sui,

Try "axis=1" to concatenate into columns, assuming factor1 .. factor4 are Pandas Series with the same Index type:

my_factors = pd.concat([factor1, factor2, factor3, factor4....]), axis=1)  

Once you have the DataFrame, you may use a simple Pandas method to return median values by rows, or by columns:

my_factors.median(axis=0)  # Median values by rows  
my_factors.median(axis=1)  # Median values by columns  

Hope this helps.

Thank you Karl Mun , it's nice of you to point out this problem, indeed, it's by axis 1

but i still get this error . TypeError: cannot concatenate a non-NDFrame object,
Maybe the probleme comes from factors

class Ebitda(CustomFactor):
inputs=[Fundamentals.enterprise_value,Fundamentals.ev_to_ebitda]
def compute(self,today,asset,out,enterprise_value,ev_to_ebitda):
out[:]=enterprise_value[0]/ev_to_ebitda[0]

ebitda=Fundamentals.enterprise_value.latest/Fundamentals.ev_to_ebitda.latest  
ebitda_1ytrl=Ebitda(window_length=252)  
ebitda_2ytrl=Ebitda(window_length=504)  
ebitda_3ytrl=Ebitda(window_length=756)  
fwd_e=(Fundamentals.shares_outstanding.latest*Fundamentals.forward_earning_yield.latest)*USEquityPricing.close.latest

ebitda_frame=pd.concat([ebitda,ebitda_1ytrl,ebitda_2ytrl,ebitda_3ytrl],axis=1)  
ebitda_median=ebitda.median(axis=1)  

Hi ce sui,

I presume you are working in Research mode.. the attached Notebook outlines the codes to assemble a series of periodic data (for example, your factors ebitda_1ytrl .. ebitda_3ytrl) using Pipeline CustomFactor into a DataFrame, to which you may add more factors and compute other values as columns using simple Pandas methods: .median() .mean() etc

Hope this helps.

thank you Karl Mun
It's very wonderful to have such a patient and skilled person as you in the forum.I hope I can help others like you one day.

No worries, ce sui :o)

I learnt a lot from Quantopian and the community who share and exchange generously - look forward to seeing you succeed and helping others too!

ps: Feel free to post and ask for help, or you may send an email to [email protected] - I find the Q team responsive and very helpful.