pandas return column name apply function for each

2019-08-12 18:12发布

I am working on the pandas dataset. For 2D dataframe try to return/append one column which return the column name whose value is over 0.95.

import pandas as pd
import numpy as np

Exp_day_list = ["EXP_DAY_1","EXP_DAY_2","EXP_DAY_3","EXP_DAY_4","EXP_DAY_5","EXP_DAY_6","EXP_DAY_7","EXP_DAY_8","EXP_DAY_9","EXP_DAY_10","EXP_GT_DAY_10"]


test = raw_databased.head()
Exp_day_percentage = test[Exp_day_list]


def over_95_percent(x):
    for column in x:
        if x[column] > 0.95:
            return column
            break
Exp_day_percentage.apply(over_95_percent,axis = 1)

I test Exp_day_percentage and result is as what I need.

Exp_day_percentage
Out[2]: 
   EXP_DAY_1  EXP_DAY_2  EXP_DAY_3  EXP_DAY_4  EXP_DAY_5  EXP_DAY_6  \
0        0.0        0.0       0.52       0.94       0.94        1.0   
1        0.0        0.0       0.00       0.66       1.00        1.0   
2        0.0        1.0       1.00       1.00       1.00        1.0   
3        0.0        0.0       0.92       1.00       1.00        1.0   
4        0.0        0.0       0.95       0.97       1.00        1.0   

   EXP_DAY_7  EXP_DAY_8  EXP_DAY_9  EXP_DAY_10  EXP_GT_DAY_10  
0        1.0        1.0        1.0         1.0            0.0  
1        1.0        1.0        1.0         1.0            0.0  
2        1.0        1.0        1.0         1.0            0.0  
3        1.0        1.0        1.0         1.0            0.0  
4        1.0        1.0        1.0         1.0            0.0  

but when I run the apply function to that dataframe, error function as following:

TypeError: ("cannot do label indexing on <class 'pandas.indexes.base.Index'>  
with these indexers [0.0] of <type 'numpy.float64'>", u'occurred at index 0')

ideal result will be following:

   EXP_DAY_1  EXP_DAY_2  EXP_DAY_3  EXP_DAY_4  EXP_DAY_5  EXP_DAY_6  \
0        0.0        0.0       0.52       0.94       0.94        1.0   
1        0.0        0.0       0.00       0.66       1.00        1.0   
2        0.0        1.0       1.00       1.00       1.00        1.0   
3        0.0        0.0       0.92       1.00       1.00        1.0   
4        0.0        0.0       0.95       0.97       1.00        1.0   

   EXP_DAY_7  EXP_DAY_8  EXP_DAY_9  EXP_DAY_10  EXP_GT_DAY_10  Column
0        1.0        1.0        1.0         1.0            0.0  EXP_DAY_5
1        1.0        1.0        1.0         1.0            0.0  EXP_DAY_5
2        1.0        1.0        1.0         1.0            0.0  EXP_DAY_2
3        1.0        1.0        1.0         1.0            0.0  EXP_DAY_4
4        1.0        1.0        1.0         1.0            0.0  EXP_DAY_3

if anyone can help me on that, I would much appreciate that. I search all internet and could not find similar thing. thank you

1条回答
Root(大扎)
2楼-- · 2019-08-12 18:23

Use pd.DataFrame.idxmax

df.assign(Column=df.gt(.95).assign(zip5=1).idxmax(1))

   EXP_DAY_1  EXP_DAY_2  EXP_DAY_3  EXP_DAY_4  EXP_DAY_5  EXP_DAY_6  EXP_DAY_7  EXP_DAY_8  EXP_DAY_9  EXP_DAY_10  EXP_GT_DAY_10     Column
0        0.0        0.0       0.52       0.94       0.94        1.0        1.0        1.0        1.0         1.0            0.0  EXP_DAY_6
1        0.0        0.0       0.00       0.66       1.00        1.0        1.0        1.0        1.0         1.0            0.0  EXP_DAY_5
2        0.0        1.0       1.00       1.00       1.00        1.0        1.0        1.0        1.0         1.0            0.0  EXP_DAY_2
3        0.0        0.0       0.92       1.00       1.00        1.0        1.0        1.0        1.0         1.0            0.0  EXP_DAY_4
4        0.0        0.0       0.95       0.97       1.00        1.0        1.0        1.0        1.0         1.0            0.0  EXP_DAY_4
查看更多
登录 后发表回答