简体   繁体   中英

How to assess all values of a row in a pandas dataframe and write into a new column

I have a pandas dataframe of 62 rows x 10 columns. Each row contains numbers and if any of the numbers are within a certain range then return a string into the last column.

I have unsuccessfully tried the.apply method to use a function to make the assessment. I have also tried to import as a series but then the.apply method causes problems because it is a list.

df = pd.read_csv(results)

For example, in the image attached, if any value from Base 2019 to FY26 Load is between 0.95 and 1.05 then return 'Acceptable' into the last column otherwise return 'Not Acceptable'.

在此处输入图像描述

Any help, even a start would be much appreciated.

This should perform as expected:

results = "input.csv"
df = pd.read_csv(results)

low = 0.95
high = 1.05

# The columns to check
cols = df.columns[2:]

df['Acceptable?'] = (df[cols] > low).any(axis=1) & (df[cols] < high).all(axis=1)

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