熊猫:得到最小列的名称

时间:2014-01-17 14:24:54

标签: python pandas

我有一个Pandas数据框如下:

incomplete_df = pd.DataFrame({'event1': [1,     2     ,np.NAN,5     ,6,np.NAN,np.NAN,11    ,np.NAN,15],
                              'event2': [np.NAN,1     ,np.NAN,3     ,4,7     ,np.NAN,12    ,np.NAN,17],
                              'event3': [np.NAN,np.NAN,np.NAN,np.NAN,6,4     ,9     ,np.NAN,3     ,np.NAN]})
incomplete_df
   event1  event2  event3
0       1     NaN     NaN
1       2       1     NaN
2     NaN     NaN     NaN
3       5       3     NaN
4       6       4       6
5     NaN       7       4
6     NaN     NaN       9
7      11      12     NaN
8     NaN     NaN       3
9      15      17     NaN

我想附加一个reason列,其中包含标准文字+ 该行最小值的列名。换句话说,所需的输出是:

   event1  event2  event3  reason
0       1     NaN     NaN  'Reason is event1'
1       2       1     NaN  'Reason is event2'
2     NaN     NaN     NaN  'Reason is None'
3       5       3     NaN  'Reason is event2'
4       6       4       6  'Reason is event2'
5     NaN       7       4  'Reason is event3'
6     NaN     NaN       9  'Reason is event3'
7      11      12     NaN  'Reason is event1'
8     NaN     NaN       3  'Reason is event3'
9      15      17     NaN  'Reason is event1'

我可以执行incomplete_df.apply(lambda x: min(x),axis=1)但这不会忽略NAN,更重要的是返回值而不是相应列的名称。

修改

根据EMS的回答了解了idxmin()函数,我给出了以下两种解决方案:

timeit.repeat("incomplete_df.apply(lambda x: x.idxmin(), axis=1)", "from __main__ import incomplete_df", number=1000)
[0.35261858807214175, 0.32040155511039536, 0.3186818508661702]

timeit.repeat("incomplete_df.T.idxmin()", "from __main__ import incomplete_df", number=1000)
[0.17752145781657447, 0.1628651645393262, 0.15563708275042387]

看起来转置方法的速度是原来的两倍。

1 个答案:

答案 0 :(得分:6)

incomplete_df['reason'] = "Reason is " + incomplete_df.T.idxmin()