简体   繁体   中英

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
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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM