gpt4 book ai didi

python - 如何将一系列数值数据转换为特定的分类数据?

转载 作者:行者123 更新时间:2023-12-01 07:05:00 24 4
gpt4 key购买 nike

我的数据在-100到100的负 float 范围内,我想以这种方式更改此数据:

from 0 to 1 - ss
from 1 to 2 - sm
from 2 to 3 - sa
> 3 s

from 0 to -1 - bs
from 1 to -2 - bm
from 2 to -3 - ba
< 3 b

如何做到这一点?

谢谢

最佳答案

请检查以下解决方案:

import pandas as pd

y = pd.DataFrame({"Data": [1.1, 1.3, 2.5, 3.9, -0.7, -1.3, -2.6, -9]})


def change_func(x):
if x <= -3:
return "b"
elif -3 < x <= -2:
return "ba"
elif -2 < x <= -1:
return "bm"
elif -1 < x <= 0:
return "bs"
elif 0 < x <= 1:
return "ss"
elif 1 < x <= 2:
return "sm"
elif 2 < x <= 3:
return "sa"
else:
return "s"


y["New_Column"] = y["Data"].apply(lambda x: change_func(x))

# if you do not want new line you can do the following:
# instead of y["New_Column"] = y["Data"].apply(lambda x: change_func(x) let's do:
# y["Data"] = y["Data"].apply(lambda x: change_func(x))

print(y)

希望对您有帮助,如果问题解决请告诉我,欢迎提问。

关于python - 如何将一系列数值数据转换为特定的分类数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58482434/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com