gpt4 book ai didi

python - Pandas 用字典替换值

转载 作者:太空宇宙 更新时间:2023-11-04 09:46:46 24 4
gpt4 key购买 nike

我有一个具有以下结构的数据框:

        Ranges  Relative_17-Aug  Relative_17-Sep  Relative_17-Oct
0 (0.0, 0.1] 1372 1583 1214
1 (0.1, 0.2] 440 337 648
2 (0.2, 0.3] 111 51 105
3 (0.3, 0.4] 33 10 19
4 (0.4, 0.5] 16 4 9
5 (0.5, 0.6] 7 7 1
6 (0.6, 0.7] 4 3 0
7 (0.7, 0.8] 5 1 0
8 (0.8, 0.9] 2 3 0
9 (0.9, 1.0] 2 0 1
10 (1.0, 2.0] 6 0 2

我正在尝试使用以下代码用字典替换列范围,但它不起作用,如果我做错了什么,有任何提示:

mydict= {"(0.0, 0.1]":"<=10%","(0.1, 0.2]":">10% and <20%","(0.2, 0.3]":">20% and <30%", "(0.3, 0.4]":">30% and <40%", "(0.4, 0.5]":">40% and <50%", "(0.5, 0.6]":">50% and <60%", "(0.6, 0.7]":">60% and <70%", "(0.7, 0.8]":">70% and <80%", "(0.8, 0.9]":">80% and <90%", "(0.9, 1.0]":">90% and <100%", "(1.0, 2.0]":">100%"}
t_df["Ranges"].replace(mydict,inplace=True)

谢谢!

最佳答案

我认为这里最好在 cut 中创建 Ranges 列时使用参数 labels :

labels = ['<=10%','>10% and <20%', ...]
#change by your bins
bins = [0,0.1,0.2...]
t_df['Ranges'] = pd.cut(t_df['col'], bins=bins, labels=labels)

如果不可能,按照注释中@Dark 的建议转换为字符串应该会有所帮助,为了获得更好的性能,请使用 map:

t_df["Ranges"] = t_df["Ranges"].astype(str).map(mydict)

关于python - Pandas 用字典替换值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49466271/

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