1

I have a data frame with the below structure:

        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

I am trying to replace column ranges with a dictionary using the below code but it is not working, any hints if I am doing something wrong:

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)

Thanks!

5
  • How do you get column Ranges ? Commented Mar 24, 2018 at 15:10
  • Perhaps casting might help t_df["Ranges"].astype(str).replace(mydict,inplace=True) Commented Mar 24, 2018 at 15:10
  • What about using map method to use the value from the dict.? Commented Mar 24, 2018 at 15:15
  • I got ranges by using groupby ranges = [0,.10,0.20,0.30,0.40,0.50,0.60,0.70,0.80,0.90,1.0,2.0] df_relative.groupby(pd.cut(df_relative["Relative_"+str(month)], ranges)).count() Commented Mar 24, 2018 at 15:17
  • I have put my full code on pastebin: pastebin.com/embed_iframe/2MqRZ68r Commented Mar 24, 2018 at 15:22

2 Answers 2

2

I think here is best use parameter labels in time of create Ranges column in cut:

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)

If not possible, cast to string should help as suggest @Dark in comments, for better performance use map:

t_df["Ranges"] = t_df["Ranges"].astype(str).map(mydict)
Sign up to request clarification or add additional context in comments.

4 Comments

Jezrael above tip works but I am still wondering why replace is not working. I used labels in pd.cut and output is as I required. Thanks
@RehanAzher - there is problem it is new IntervalIndex, so possible solution is cast to strings. But cut + labels is better solution.
Yes, cut + label is better solution.
Delima how to accept both as solution , both are working :)
2

By using map function this can be achieved easily and in a straight forward manner as shown below..

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)])

Hope this helps..!!

7 Comments

t_df["Ranges"].map( mydict)
Yep, I was just using lambda's it is more like an habit..!!
Below worked : ` t_df["Ranges"] = t_df["Ranges"].map(lambda x : mydict[str(x)]) `
Good catch, I was using str ranges for trying out the code.
Sorry, I had to accept @Jezrael answer as the solution not only he/she provided a solution but right away knew how i got the ranges.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.