根据条件与数据框列不同的新列

时间:2020-10-19 17:09:36

标签: python pandas dataframe

我有两个数据集

df1 = pd.DataFrame({"skuid" :["A","B","C","D","E"], "price": [0,0,0,0,0]})
df2 = pd.DataFrame({"skuid" :["A","B","C","D"],"salesprice" :[10,0,np.nan,30],"regularprice" : [9,10,0,2]})

我想在条件中插入销售价格和正常价格: 如果df1 skuid和df2 skuid匹配并且df2 salesprice不为零,请使用salesprice作为价格值。如果sku的匹配项和df2 salesprice为零,则使用Regularprice。如果不使用零作为价格值。

def pric(df1,df2):
    if (df1['skuid'] == df2['skuid'] and salesprice !=0): 
        price = salesprice 
    elif (df1['skuid'] == df2['skuid'] and regularprice !=0):
        price = regularprice
    else:
        price = 0

我在类似的条件下创建了一个函数,但是它不起作用。结果应类似于df1

skuid  price
  A      10
  B      10
  C      0
  D      30
  E      0

谢谢。

3 个答案:

答案 0 :(得分:1)

您可以使用merge,但首先使用.loc将salesprice值更改为等于零的Regularprice值。最终使用`.fillna(0)满足剩余条件:

df1 = pd.DataFrame({"skuid" :["A","B","C","D","E"], "price": [0,0,0,0,0]})
df2 = pd.DataFrame({"skuid" :["A","B","C","D"],"salesprice" :[10,0,np.nan,30],"regularprice" : [9,10,0,2]})
df = df2.copy()
df.loc[df['salesprice'] == 0, 'salesprice'] = df['regularprice']
df = pd.merge(df1[['skuid']],
              df[['skuid','salesprice']].rename({'salesprice':'price'}, axis=1),
              how='left', on='skuid').fillna(0)
df
Out[1]: 
  skuid  price
0     A   10.0
1     B   10.0
2     C    0.0
3     D   30.0
4     E    0.0

答案 1 :(得分:1)

您可以使用df.mergenp.select

In [1228]: import numpy as np

In [1229]: res = df1.merge(df2, on='skuid', how='outer')

In [1230]: conditions = [(res.salesprice.notna() & res.regularprice.notna()) & res.salesprice.ne(0), (res.salesprice.notna() & res.regularprice.notna()) & res.salesprice.eq(0)]    
In [1233]: choices = [res.salesprice, res.regularprice]

In [1235]: res['price'] = np.select(conditions, choices) 
In [1238]: res.drop(['salesprice', 'regularprice'], axis=1, inplace=True)

In [1239]: res
Out[1239]: 
  skuid  price
0     A   10.0
1     B   10.0
2     C    0.0
3     D   30.0
4     E    0.0

答案 2 :(得分:1)

感谢大家的回答,同时我也想出了一种方法,

我使用了gen_random_uuidmerge

fillna