简体   繁体   English

根据现有列中的 2 个条件创建新的 pandas 数据框

[英]Create new pandas data frame based on 2 conditions in an existing column

Easy question here: I am creating a new data frame based on a report of contracts where I only want 2 statuses.这里有一个简单的问题:我正在根据合同报告创建一个新的数据框,我只需要 2 个状态。 I have the first one, but am having trouble adding the second condition.我有第一个,但我无法添加第二个条件。

df2 = df[(df['[PCW] Contract Status'] == "Draft")]

The other status is "Draft Amendment".另一个状态是“修正案草案”。 So I basically want it to read like所以我基本上希望它读起来像

df2 = df[(df['[PCW] Contract Status'] == "Draft", "Draft Amendment")]

You can use isin() .您可以使用isin()

df2 = df[df['[PCW] Contract Status'].isin(["Draft", "Draft Amendment"])]

Or else you can create the list of required variables earlier and then add the name of the list in isin() .或者您可以更早地创建所需变量的列表,然后在isin()中添加列表的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 基于现有列在 pandas 数据框中创建新列 - Create new columns in pandas data frame based on existing column 根据同一数据框中的多个条件创建新列 - Create new column based on multiple conditions in the same data frame 使用 Pandas 从现有列创建新列到数据框 - Create a new column to data frame from existing columns using Pandas 如何基于现有列熊猫的多个条件创建新列 - How to create new column based on multiple conditions from existing column pandas pandas dataframe 中的新列基于现有列值和条件列表 - New column in pandas dataframe based on existing column values with conditions list python:根据现有数据框列(日期)创建新的数据框 - python : create new data frame based on existing data frame column ( date ) 根据多个条件将现有列的值分配给 Pandas 中的新列 - Assign value of existing column to new columns in pandas based on multiple conditions 根据 pandas df 中的条件在现有列中插入新行 - insert a new row in the existing column based on conditions in pandas df Pandas:在数据框中创建一个新列,其中的值是从现有列 i 计算出来的。 计算最大值 - Pandas: Create a new column in a data frame with values calculated from an already existing column, i. calculate maximum 根据条件从现有数据框创建新数据框 - create a new data frame from existing data frame based on condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM