Pandas - 在保留 NaN 的同时合并数值列

时间:2021-01-17 09:20:23

标签: pandas binning

我在数据框中有一列包含数值(例如年龄),并希望将其分为几个类别(或范围),如下所示:

df['Age_binned'] = pd.qcut(df['Age'], 5, duplicates='drop')

产生以下范围:

(-0.001, 36.0]
(36.0, 45.0]
(45.0, 54.0]
(54.0, 62.0]
(62.0, 87.0]

但是,我知道该列还包含一些 NaN 值,我想将其视为单独的 bin。我怎样才能做到这一点?

我厌倦了使用 .fillna(),但如果我用数值填充 NaN,它会与上述 bin 合并,而非数值似乎被 .qcut() 方法忽略。

1 个答案:

答案 0 :(得分:0)

您可以用 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <div class="draggable" class="ui-widget-content"> <p>Drag me to my target</p> </div> <div class="draggable" class="ui-widget-content"> <p>Drag me to my target</p> </div> <div class="draggable" class="ui-widget-content"> <p>Drag me to my target</p> </div> <div class="droppable" class="ui-widget-header"> <p>Drop here</p> </div> 填充 nan:

np.nan