简体   繁体   English

Pandas根据行中其他单元格的值为单元格赋值

[英]Pandas assign value to cell based on values of other cells in row

Given the following data frame: 给出以下数据框:

import pandas as pd
import numpy as np
DF = pd.DataFrame({'COL1': ['a','b','b'], 
                   'COL2' : [0,np.nan,1],})

DF

    COL1    COL2
0    a        0      
1    b       NaN     
2    b        1      

I want to be able to assign a new column COL3 that has a value of 2 for every row where COL1 is b and COL2 is not null. 我希望能够为COL1bCOL2不为空的每一行分配一个值为2的新列COL3

The desired result is as follows: 期望的结果如下:

    COL1    COL2    COL3
0    a        0      0
1    b       NaN     0
2    b        1      2

Thanks in advance! 提前致谢!

You can use numpy.where with isin and notnull : 您可以使用numpy.whereisinnotnull

DF['COL3'] = np.where((DF['COL1'].isin(['b'])) &(DF['COL2'].notnull()), 2, 0)
print DF 


  COL1  COL2  COL3
0    a     0     0
1    b   NaN     0
2    b     1     2

This can be achieved using the apply method on the DataFrame. 这可以使用DataFrame上的apply方法来实现。 You'll need to pass in a function to apply to each row and set the axis to 1 to set it to the correct mode (apply for each row, instead of for each column). 您需要传入一个函数以应用于每一行并将轴设置为1以将其设置为正确的模式(应用于每一行,而不是每列)。

Here's a working example: 这是一个有效的例子:

def row_handler(row):
    if row['COL1'] == 'b' and not np.isnan(row['COL2']):
        return 2
    return 0

DF['COL3'] = DF.apply(row_handler, axis=1)

Which returns this: 哪个返回:

>> print DF
  COL1  COL2  COL3
0    a     0     0
1    b   NaN     0
2    b     1     2

Define a function to return your value based on other columns. 定义一个函数以根据其他列返回值。

def value_handle (row):
    if row['COL1'] == 'b' and not pd.isnull(row['COL2']) :
    return 2
else:
    return 0

Then call the new function when introducing the new column. 然后在引入新列时调用新函数。

DF['COL3'] = DF.apply (lambda row: value_handle (row),axis=1)

暂无
暂无

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

相关问题 基于 Pandas DataFrame 行中的其他值突出显示一行中的多个单元格 - Highlighting multiple cells in a row based on other values in the row in a Pandas DataFrame 根据其他列单元格的值设置熊猫DataFrame单元格的内容 - Setting the content of a pandas DataFrame cell based on the values of other columns cells 根据 Pandas Dataframe 中的其他单元格查找单元格值 - Find the cell value based on other cells in Pandas Dataframe 如何根据另一个单元格的值填充先前单元格的值? - How to fill the values of previous cells based on the value of the other cell? 如何根据随后的行单元格值更改数据帧单元格的值? - How to change values of dataframe cells based on following row cell value? Python/Pandas:根据多个列/行值为列赋值 - Python/Pandas: assign a value to a column based on multiple column/row values 如何根据先前的单元格值优化更新单元格/如何将单元格的值优雅地传播到其他单元格? - How to optimally update cells based on previous cell value / How to elegantly spread values of cell to other cells? Pandas:根据另一行中的值分配值 - Pandas: Assign value based on value in another row 遍历Pandas数据框单元以根据其他单元值计算运行总计 - Iterate over Pandas dataframe cells to calculate running totals based on other cell values Python / Pandas-如何为每行分配值,具体取决于单元格的值 - Python/Pandas - How to assign value for each row which depends on cells values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM