1

I have a df called 'records' filled with numbers (0-5) as shown.

One of its column is shown with values 0-3.

enter image description here

enter image description here

I want to loop through the dataframe and change all values above 1 to 1 so there will be only 0s and 1s. I came up with this line of code but unsure how to iterate through all df values with apply:

records= records.apply(lambda x: 1 if x>1 for x in "something")
1
  • Consider avoid saving data elements as column names. Unless doing matrix operations, your data frame should be in long format of two columns: indicator and value. Commented Sep 25, 2021 at 14:47

4 Answers 4

3

Convert as bool and as int:

records = records.astype(bool).astype(int)

Example:

>>> records
   A  B  C  D  E
0  2  3  4  1  5
1  0  0  2  5  2
2  1  1  2  0  3
3  0  3  5  4  1
4  2  3  2  0  3
5  2  1  5  2  0
6  0  3  1  3  4
7  1  3  3  2  2
8  1  3  1  5  1
9  4  5  3  4  4

>>> records.astype(bool).astype(int)
   A  B  C  D  E
0  1  1  1  1  1
1  0  0  1  1  1
2  1  1  1  0  1
3  0  1  1  1  1
4  1  1  1  0  1
5  1  1  1  1  0
6  0  1  1  1  1
7  1  1  1  1  1
8  1  1  1  1  1
9  1  1  1  1  1
Sign up to request clarification or add additional context in comments.

Comments

1

You can also use df.where:

df = pd.DataFrame({'A': [1, 0, 2, 5],
                   'B': [0, 2, 3, 4],
                   })

print (df.where(df==0, 1))

   A  B
0  1  0
1  0  1
2  1  1
3  1  1

Comments

0

Also, provide the axis=1 argument in the apply method.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

You can also use the .clip() function, setting upper limit equals 1, as follows:

records = records.clip(upper=1)

Taking Corralien's sample data, we have

print(records)

   A  B  C  D  E
0  2  3  4  1  5
1  0  0  2  5  2
2  1  1  2  0  3
3  0  3  5  4  1
4  2  3  2  0  3
5  2  1  5  2  0
6  0  3  1  3  4
7  1  3  3  2  2
8  1  3  1  5  1
9  4  5  3  4  4

records = records.clip(upper=1)

print(records)

   A  B  C  D  E
0  1  1  1  1  1
1  0  0  1  1  1
2  1  1  1  0  1
3  0  1  1  1  1
4  1  1  1  0  1
5  1  1  1  1  0
6  0  1  1  1  1
7  1  1  1  1  1
8  1  1  1  1  1
9  1  1  1  1  1

Comments

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.