熊猫用字典替换值

时间:2018-03-24 15:09:02

标签: python pandas dictionary dataframe

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

        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)

谢谢!

2 个答案:

答案 0 :(得分:2)

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

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)

答案 1 :(得分:2)

通过使用map功能,可以轻松地以直接的方式实现,如下所示。

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"] = t_df["Ranges"].map(lambda x : mydict[str(x)])

希望这会有所帮助.. !!