Populate column C , if condition from Column A and Column B meets pandas

Viewed 37

I have a dataset that looks like this

  Contract Sections Trade_Qty_0  TradeQty     Bid      iBid 
5     O.Z21       O2          no     42985  99.795       NaN   
6     O.H22       O2          no     54538  99.810  99.81000   
7     O.M22       O2          no     31295  99.795  99.79500   
8    S3.U20       S3          no      3831   3.000       NaN   
9    S3.Z20       S3          no      3295  -9.500  -9.25000   
10   S6.M26       S6         yes         0     NaN   7.75000   
11   S6.U26       S6         yes         0     NaN   5.50000   
12   S6.Z26       S6         yes         0     NaN   5.50000   
13   S6.H27       S6         yes         0     NaN   3.50000   
14   S9.U26       S9         yes         0     NaN   9.00000   
15   S9.Z26       S9         yes         0     NaN   7.50000   
16   F3.U26       F3         yes         0   1.000       NaN   
17   F3.Z26       F3         yes         0  -3.000       NaN  

What I want to do is check if TradeQty is 0? If it is 0 I want to check if Bid is populated or not. If it is not populated I want to print 'No'in a new column say column C. However, if it is populated I want to print 'yes'. So I want my column C to look something like

Column C'
NAN
NAN
NAN
NAN
NAN
no
no
no
no
no
no
yes
yes

What I'm doing is assigning values this way

A016 = A016.assign(Trade_Qty_0_Bid_present=((A016.TradeQty== 0) & (A016.Bid.notnull())))
A016.Trade_Qty_0_Bid_present.replace(True, 'Yes', inplace=True)
A016.Trade_Qty_0_Bid_present.replace(False, 'no', inplace=True)

but the answer I'm getting is

Column C'
no
no
no
no
no
no
no
no
no
no
no
yes
yes
1 Answers

You can use another assignment to replace the values with NaN.

A016 = A016.assign(Trade_Qty_0_Bid_present=((A016.TradeQty== 0) & (A016.Bid.notnull())))
A016.Trade_Qty_0_Bid_present.replace(True, 'Yes', inplace=True)
A016.Trade_Qty_0_Bid_present.replace(False, 'no', inplace=True)
A016.loc[A016.TradeQty!=0, "Trade_Qty_0_Bid_present"] = pd.NA 
Related