I have a df called 'records'
filled with numbers (0-5) as shown.
One of its column is shown with values 0-3.
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")
CodePudding user response:
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
CodePudding user response:
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
CodePudding user response:
Also, provide the axis=1
argument in the apply method.
CodePudding user response:
You can use .ne(0)
like below:
>>> df = pd.DataFrame({'A': [1, 0, 2, 5],'B': [0, 2, 3, 4], 'C': [3,4,0,0], 'D':[0,1,1,1]})
>>> df
A B C D
0 1 0 3 0
1 0 2 4 1
2 2 3 0 1
3 5 4 0 1
>>> df[df.ne(0)] = 1
>> df
A B C D
0 1 0 1 0
1 0 1 1 1
2 1 1 0 1
3 1 1 0 1
CodePudding user response:
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