如何根据另一个单元格的值有条件地填充一个单元格

How to conditionally fill a cell based on the value of another cell

对于 ProductName 值为 Salad 的行,我想用“有效”一词填充“SaleTypeName”列的 NaN 单元格值。

到目前为止我已经走了这么远: 销售['SaleTypeName'].fillna("Valid", inplace=True) 但是我在这里缺少一个条件方面,因此当 NaN 出现时不会填充整个列

我有以下数据:

import pandas as pd
df = pd.DataFrame ({'Flight Day': ['2018-10-01', '2018-10-01','2018-10-01', '2018-10-01', '2018-10-02','2018-10-02', '2018-10-02', '2018-10-02', '2018-10-03','2018-10-03’, '2018-10-03','2018-10-03’],'Flight Number': [LX1336, LX1337, LX1472, LX1473, LX1336, LX1337, LX1472, LX1473, LX1336, LX1337, LX1472, LX1473],'STD Departure': [10, 15, 8, 11,10, 15, 8, 14,10, 15, 8, 11], ‘Quantity': [2, NaN, 8, 4,3, 2, 3, 1,NaN, 5, 2, 1],’ProductName': [Bircher, Salad, Carac, Salad, Bircher, Carac, Carac, Salad,Bircher, Carac, Carac, Salad], 'SaleTypeName': [NaN, NaN, NaN, NaN ,NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN]})

对于这个特殊的问题,您可以试用这段代码,稍后您可以进行相应的修改。

数据框:

import pandas as pd
import numpy as np
df = pd.DataFrame (
    {'Flight Day': ['2018-10-01', '2018-10-01','2018-10-01', '2018-10-01', '2018-10-02','2018-10-02', '2018-10-02', '2018-10-02', '2018-10-03','2018-10-03', '2018-10-03','2018-10-03'],
     'Flight Number': ['LX1336', 'LX1337', 'LX1472', 'LX1473', 'LX1336', 'LX1337', 'LX1472', 'LX1473', 'LX1336', 'LX1337', 'LX1472', 'LX1473'],
     'STD Departure': [10, 15, 8, 11,10, 15, 8, 14,10, 15, 8, 11],
     'Quantity': [2, np.NaN, 8, 4,3, 2, 3, 1,np.NaN, 5, 2, 1],
     'ProductName': ['Bircher',' Salad',' Carac',' Salad',' Bircher',' Carac',' Carac',' Salad','Bircher',' Carac',' Carac',' Salad'],
     'SaleTypeName': [np.NaN, np.NaN, np.NaN, np.NaN ,np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN]})

要实现的代码部分:

def replace(name):
    return "valid" if name.strip()=="Salad" else np.NaN

df['SaleTypeName'] = df.loc[:,'ProductName'].apply(lambda x: replace(x))

输出:

    ProductName SaleTypeName
0      Bircher          NaN
1        Salad        valid
2        Carac          NaN
3        Salad        valid
4      Bircher          NaN
5        Carac          NaN
6        Carac          NaN
7        Salad        valid
8      Bircher          NaN
9        Carac          NaN
10       Carac          NaN
11       Salad        valid

你可以使用

df.SaleTypeName.mask(df.ProductName=='Salad', 'Valid', inplace=True)

mask()函数用于select将ProductName列中"Salad"的行SaleTypeName的值替换为"Valid" .

inplace 参数用于对原始数据框本身进行更改。

docs