在熊猫数据框中对数值进行分类

时间:2020-11-11 22:01:46

标签: python pandas dataframe lambda rounding

我有一个pandas数据帧,如下所示:

Y      HePrecip
0      0
0.14   0.19
0.53   0.62
0.86   0.90

我想通过将值分组为四分之一间隔来对HePrecip列进行分类(例如[0,.25],[。26,.50],[。51,.75],[。75, 1.0],...)

我编写了以下lambda函数和辅助方法来尝试实现此目的

def my_round(x):
  return math.ceil(x * 4) / 4

df['Categories'] = df.apply(lambda x: my_round(x['HePrecip']), axis=1)

例如,目标是将.19舍入为.25,.62舍入为.75,.76舍入为1.0。

但是,我得到一个SettingWithCopyWarning。这样可以吗?

/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:1: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  """Entry point for launching an IPython kernel.

0 个答案:

没有答案