LinnK
LinnK

Reputation: 395

If test on Pandas DataFrame

I would like to do an if test on the 'ave_data' DataFrame below:

ave_data=

Time ------------ F7 --------------- F8 --------------- F9
00:00:00 ---- 43.005593 ---- -56.509746 ---- 25.271271
01:00:00 ---- 55.114918 ---- -59.173852 ---- 31.849262
02:00:00 ---- 63.990762 ---- -64.699492 ---- 52.426017

Specifically, I would like to test if any of the values are less than 0.05 and output 1 if they are not, and 0 if they are, so that the output (named, for example, 'tested_ave_data') looks like this:

tested_ave_data=

Time ------------ F7 --- ------- F8 ---------- F9
00:00:00 -------- 1 ------------ 0 ------------ 1
01:00:00 -------- 1 ------------ 0 ------------ 1
02:00:00 -------- 1 ------------ 0 ------------ 1

Can anyone help me develop a code to do this? I've searched for ways to do this for a long time now but not been successful.

Upvotes: 0

Views: 60

Answers (1)

user2489252
user2489252

Reputation:

you don't need to "develop" any code if I understand your question correctly. Just use a Boolean mask -- everything is already implemented in pandas.

import pandas as pd

a = [[2., .3, 4., 5.], [.8, .03, 0.02, 5.]]
df = pd.DataFrame(a)
df

enter image description here

df <= 0.05

enter image description here

df = df < 0.05
df.astype(int)

enter image description here

Upvotes: 1

Related Questions