简体   繁体   中英

How can I add a column to a dataframe that is based on another columns value?

For school I have a DataFrame (containing of multiple columns and rows) and my task is to create a new column in that DataFrame that contains 'true' if the value of a column in that dataframe is higher than a certain value and the new column should contain 'false' if the value is lower than the value from a column in the dataframe. Can somebody please help me out? (I'm supposed to do this in jupyter-notebook)

Try:

import numpy as np
df[<new col>] = np.where(df[<value col>] > <threshold>, True, False)

But replace <new col> with the string name of your new column, <value col> with the string name of the column you're comparing, and replace <threshold> with the compared value.

Try

df['flag'] = True
df.loc[df['A']>2,'flag'] = 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