简体   繁体   English

在 Python 中使用具有特定条件的多列映射值

[英]Map values using multiple columns with a specific condition in Python

I have a dataset where I would like to map values based on a specific condition.我有一个数据集,我想根据特定条件映射值。 I would like to add a new column and then map a label to an ID if it meets the condition of:我想添加一个新列,然后将一个标签映射到一个 ID,如果它满足以下条件:

**If ID == AA AND Date >= to Q121:  Status = 'closed' AND values within the Used column will be null.**

Data数据

ID  Date    Location    Used    
AA  Q121    NY          20  
AA  Q221    NY          50  
AA  Q321    NY          10  
BB  Q121    CA          1   
BB  Q221    CA          0   
BB  Q321    CA          500 
BB  Q421    CA          700 
CC  Q121    AZ          50  

Desired期望的

ID  Date    Location    Used    Status
AA  Q121    NY                  closed
AA  Q221    NY                  closed
AA  Q321    NY                  closed
BB  Q121    CA          1   
BB  Q221    CA          0   
BB  Q321    CA          500 
BB  Q421    CA          700 
CC  Q121    AZ          50

Doing正在做

df['Status']=df['ID'].map({'AA':'closed' ,  })

Is it possible to map using two columns, or would a loop be better fit for this?是否可以使用两列进行映射,或者循环更适合这个? Any suggestion is appreciated.任何建议表示赞赏。

You can use np.where for this:您可以为此使用np.where

df['Used'] = np.where(((df.ID == 'AA') & (df.Date >= 'Q121')), '', df['Used'])
df['Status'] = np.where(((df.ID == 'AA') & (df.Date >= 'Q121')), 'closed', '')

# or use np.nan instead of '' above, that's actually preferable (generating NaN values)

print(df)

   ID  Date Location   Used  Status
0  AA  Q121       NY         closed
1  AA  Q221       NY         closed
2  AA  Q321       NY         closed
3  BB  Q121       CA    1.0        
4  BB  Q221       CA    0.0        
5  BB  Q321       CA  500.0        
6  BB  Q421       CA  700.0        
7  CC  Q121       AZ   50.0         

暂无
暂无

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

相关问题 使用 Pandas (Python) 基于单个条件将值分配给多个列 - Assigning values to multiple columns based on a single condition using Pandas (Python) 使用 Python 中的条件从特定列中删除值 - Remove values from specific columns with a condition in Python 使用loc和map更改多个列中的值 - Using loc and map to change values in multiple columns Python数据框:根据特定条件合并列的值 - Python dataframes: Merge values of columns according to a specific condition select 值基于 pandas dataframe 中的 pandas 中的条件 - select values based on condition on multiple columns for pandas dataframe in python Python - 根据其他列的多个条件转换列的值 - Python - convert values of a column based on multiple condition of other columns 根据多列值的条件,使用索引和广播一次覆盖多列中的值 - Overwrite values in multiple columns at once based on condition of values in multiple columns, using indexing and broadcasting Pandas df:用另一列中的特定值填充新列中的值(具有多列的条件) - Pandas df: fill values in new column with specific values from another column (condition with multiple columns) 有没有办法在条件中使用多种数据类型过滤 Python 中的列? - Is there a way to filter columns in Python using multiple data types in condition? Python(xlrd):如何从Excel中获取多个特定列的值? - Python (xlrd): How to get values of multiple specific columns from an Excel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM