user308827
user308827

Reputation: 21971

Pandas replace issue

I can use pandas replace to replace values in a dataframe using a dictionary:

prod_dict     = {1:'Productive',2:'Moderate',3:'None'}
df['val'].replace(prod_dict,inplace=True)

What do I do if I want to replace a set of values in the dataframe with a single number. E.g I want to map all values from 1 to 20 to 1; all values from 21 to 40 to 2 and all values from 41 to 100 to 3. How do I specify this in a dictionary and use it in pandas replace?

Upvotes: 0

Views: 310

Answers (1)

user4179775
user4179775

Reputation:

You can do that using apply to traverse and apply function on every element, and lambda to write a function to replace the key with the value of in your dictionary.

I will go through a quick example here.

  • First, I will create a dataframe to showcase the algorithm
df = pd.DataFrame(range(50), columns=list('B'))
  • This function should generate a list of values between i,j .
def genValues(i,j):
  return [x for x in range(j+1) if x >=i]
  • I will create lambda function to map the values.
df['E']= df['B'].apply(lambda x: 1 if x in genValues(0,20) else 2 if x in genValues(21,40) else 3 if x in genValues(41,100) else x)

print df

The output:

     B  E
0    0  1
1    1  1
2    2  1
3    3  1
4    4  1
5    5  1
6    6  1
7    7  1
8    8  1
9    9  1
10  10  1
11  11  1
12  12  1
13  13  1
14  14  1
15  15  1
16  16  1
17  17  1
18  18  1
19  19  1
20  20  1
21  21  2
22  22  2
23  23  2
24  24  2
25  25  2
26  26  2
27  27  2
28  28  2
29  29  2
30  30  2
31  31  2
32  32  2
33  33  2
34  34  2
35  35  2
36  36  2
37  37  2
38  38  2
39  39  2
40  40  2
41  41  3
42  42  3
43  43  3
44  44  3
45  45  3
46  46  3
47  47  3
48  48  3
49  49  3

You can replace the column by replacing it:

 df['B']= df['B'].apply(lambda x: 1 if x in genValues(0,20) else 2 if x in genValues(21,40) else 3 if x in genValues(41,100) else x)

Upvotes: 2

Related Questions