Goutam
Goutam

Reputation: 423

How to restrict my dataframe for all the columns values between 0 to 100 in python

Can anyone help me out to restrict my dataframe for all the columns values between 0 to 100 in python:

if (df.loc[:,:] > 0) & (df.loc[:,:] < 100):
    print(df)
else:
    print('Data out of range')

Also, please find the dataframe df below:

Position    Marks
5.7680  56.4000
5.7680  56.4000
5.7680  56.4000
5.7680  56.4000
7.2680  56.4000
5.7680  56.4000
5.7680  56.4000
5.7680  
5.7680  56.4000
5.7680  56.4000
56.400
5.7680  56.4000
5.9680  56.4000
5.7680  56.4000
2.7680  56.4000
5.7680  56.4000
5.7680  
5.7680  56.4000
5.7680  56.4000
5.7680  56.4000

Upvotes: 0

Views: 1258

Answers (2)

ipj
ipj

Reputation: 3598

After clarifying question I've ended with this one line of code:

print("DATA OUT OF RANGE" if  df[(0<df)&(df<100)].isnull().values.any() else df)

Now above solution should meet requirements.

Upvotes: 1

xzelda
xzelda

Reputation: 182

df = pd.DataFrame({'birth_year': [1979, 50, 1977],
                'Price': [2000, 300, 375] })
df[(0<df)&(df<100)]

should work. Df is just a example

Based on the new information provided, this is more correct

df = df[(0<df)&(df<100)]
sum_outside_0_100 = df.isna().sum().sum()
if sum_outside_0_100>0:
   print("DATA OUT OF RANGE")
else: 
   print(df)

Upvotes: 1

Related Questions