0

I'm trying to add a new column calculated from first quartile of all columns. something like this:

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

and this is the error I receive:

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"

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.

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

2
  • What is df.columns? Commented Oct 4, 2016 at 15:23
  • @NickilMaveli Int64Index([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29], dtype='int64', name='Day') Commented Oct 4, 2016 at 15:26

1 Answer 1

1

Try this out :

df['Q25']= [np.percentile(df.loc[i,:], 25) for i in df.index]
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the answer but it doesn't work: TypeError: unhashable type: 'slice'
Another point: your code uses index ( rows) while I want to consider columns. I changed your code to this : df['Q25']= [np.percentile(df[:,i], 25) for i in map(lambda l : str(l), df.columns)] and received the same error : TypeError: unhashable type: 'slice'
You cannot add a column to your df of size the size of df.columns. It does not mean anything. The shapes are not the same. You might want to add a new row ...
Thanks again , but also the new code (without changing it for columns) results in the same error
My bad, I tried it again and it's working. The bad news (for me!) is that for the rows containing NaN, the Q25 value is NaN but it's a different issue and I'll try to search it. Thanks a lot for your help - And sorry that I can't vote up your answer, my reputation is so low for that!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.