[英]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. 对于学校,我有一个DataFrame(包含多个列和行),并且我的任务是在该DataFrame中创建一个包含“ true”的新列(如果该数据框中的列的值大于某个值,则该新列如果该值小于数据框中某列的值,则应包含“ false”。 Can somebody please help me out? 有人可以帮我吗? (I'm supposed to do this in jupyter-notebook) (我应该在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. 但是,将<new col>
替换为<new col>
的字符串名称,将<value col>
替换为要比较的列的字符串名称,然后将<threshold>
替换为比较的值。
Try 尝试
df['flag'] = True
df.loc[df['A']>2,'flag'] = False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.