且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python:基于 Pandas 中 2 列的分箱

更新时间:2023-12-01 09:56:28

Using np.where

In [4510]: df['group'] = np.where((df.height <= 400) & (df.width <= 300),
      ...:          'g1',
      ...:          np.where((df.height <= 640) & (df.width <= 480),
      ...:          'g2',
      ...:          np.where((df.height <= 800) & (df.width <= 600),
      ...:          'g3',
      ...:          np.where((df.height > 800) & (df.width > 600),
      ...:          'g4',
      ...:          'others'))))

In [4511]: df
Out[4511]:
                              filename  height   width   group
0        shopfronts_23092017_3_285.jpg   750.0   560.0      g3
1                   shopfronts_200.jpg  4395.0  6020.0      g4
2  shopfronts_25092017_eateries_98.jpg   414.0   621.0  others
3                   shopfronts_101.jpg   480.0   640.0  others
4                   shopfronts_138.jpg  3733.0  8498.0      g4
5  shopfronts_25092017_eateries_95.jpg   187.0   250.0      g1
6      shopfronts_25092017_neon_33.jpg   100.0   200.0      g1
7                   shopfronts_322.jpg   682.0  1024.0  others
8                   shopfronts_171.jpg   800.0   600.0      g3
9         shopfronts_23092017_3_35.jpg   120.0   210.0      g1