简体   繁体   中英

how to compare columns within dataframe python?

I'm new to Python. I have a dataframe, and I want to filter it for the times values on Column A have come within 2% range of values on Column B.

So something like (i guess):

df_[df_['B']*0.98<=df_['A']<=df_['B']*1.02] #this prints an error

len(df_[df_['B']*0.98<=df_['A']<=df_['B']*1.02]) #this prints an error

You need to write conditions separately, put them each in brackets and combine with the & operator. Try

df = df[(df['B'] <= df['A']*1.02) & (df['A'] <= df['B']*1.02)]

to create a columns in you data frame 'condition_verification'

import numpy as np
df('condition_verification')=np.where((df['B'] <= df['A']*1.02)& (df['A'] <= df['B']*1.02), True, False)

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