Home > front end >  How to change several values of pandas DataFrame at once?
How to change several values of pandas DataFrame at once?

Time:07-13

Let's consider very simple data frame:

import pandas as pd
df = pd.DataFrame([[0, 1, 2, 3, 2, 5], [3, 4, 5, 0, 2, 7]]).transpose()
df.columns = ["A", "B"]

   A  B
0  0  3
1  1  4
2  2  5
3  3  0
4  2  2
5  5  7

I want to do two things with this dataframe:

  1. All numbers below 3 has to be changed to 0
  2. All numbers equal to 0 has to be changed to 10

The problem is, that when we apply:

df[df < 3] = 0
df[df == 0] = 10

we are also going to change numbers which were initially not 0, obtaining:

    A   B
0   10  3
1   10  4
2   10  5
3   3   10
4   10  10
5   5   7

which is not a desired output which should look like this:

    A   B
0   10  3
1   0   4
2   0   5
3   3   10
4   0   0
5   5   7

My question is - is there any opportunity to change both those things at the same time? i.e. I want to change numbers which are smaller than 3 to 0 and numbers which equal to 0 to 10 independently of each other.

Note! This example is created to just outline the problem. An obvious solution is to change the order of replacement - first change 0 to 10, and then numbers smaller than 3 to 0. But I'm struggling with a much complex problem, and I want to know if it is possible to change both of those at once.

CodePudding user response:

Use applymap() to apply a function to each element in the DataFrame:

df.applymap(lambda x: 10 if x == 0 else (0 if x < 3 else x))

results in

    A   B
0   10  3
1   0   4
2   0   5
3   3   10
4   0   0
5   5   7

CodePudding user response:

I would do it following way

import pandas as pd
df = pd.DataFrame([[0, 1, 2, 3, 2, 5], [3, 4, 5, 0, 2, 7]]).transpose()
df.columns = ["A", "B"]
df_orig = df.copy()
df[df_orig < 3] = 0
df[df_orig == 0] = 10
print(df)

output

    A   B
0  10   3
1   0   4
2   0   5
3   3  10
4   0   0
5   5   7

Explanation: I use .copy method to get copy of DataFrame, which is placed in variable df_orig, then use said DataFrame, which is not altered during run of program, to select places to put 0 and 10.

  • Related