Pandas : Apply function on multiple columns
<p>I'm trying to add a new column calculated from first quartile of all columns.
something like this:</p>

<pre><code>df['Q25']=df[col].apply(lambda x: np.percentile(x, 25) ,axis =1)
#col = ['1', '2','3',..'29'] days in one month
</code></pre>

<p>and this is the error I receive:</p>

<pre><code>KeyError: "['1' '2' '3' '4' '5' '6' '7' '8' '9' '10' '11' '12' '13' '14' '15' '16'\n '17' '18' '19' '20' '21' '22' '23' '24' '25' '26' '27' '28' '29'] not in index"
</code></pre>

<p>I understand that error shows python is trying to find passed list (col) in the index, instead of columns itself but I don't know how should I fix it. I already added axis=1 but with no luck.</p>

<p>PS. I can't add column manually like <code>df['1'], df['2']</code> since the total number (in this case 29) changes in other cases.</p>

<p>Try this out :</p>

<pre><code>df['Q25']= [np.percentile(df.loc[i,:], 25) for i in df.index]
</code></pre>

python pandas