简体   繁体   English

如何将一列添加到基于另一个列值的数据框中?

[英]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.

相关问题 如何将列添加到 dataframe 中,其值取决于另一个 dataframe? - How can I add a column to a dataframe with a value conditional on another dataframe? 如何将基于列的 dataframe 中的值添加到基于行的另一个 dataframe 中? - How do I add the value from one dataframe based on a column to another dataframe based on a row? 我如何根据另一个具有不同长度但共享列数据的 dataframe 的条件向 dataframe 添加一列 - How can i add a column to a dataframe based on a conditional of another dataframe that has a different length, but shared column data 如何根据另一个 dataframe 的匹配为 dataframe 的新列添加值? - how to add value to a new column to a dataframe based on the match of another dataframe? 如何根据 Pandas 数据框中的另一列值添加列? - How to add column based on another column value in Pandas dataframe? 如何根据另一个列值对两个数据框列求和 - How to sum two dataframe columns based on another column value 根据另一列中的值组合数据框的列 - Combining columns of dataframe based on value in another column 如何使用 Pandas 根据数据框中另一列的值获取 2 列的总和 - How do I use Pandas to get the sum of 2 columns based on the value of another column in a dataframe 如何根据另一列中是否满足一组条件向 Python 中的数据框添加新列? - How can I add a new column to a dataframe in Python based on whether a set of conditions are met in another column? 如何在另一个 dataframe 中根据条件添加列值? - How to add column value based on condition in another dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM