I have a pandas df like below
tno,tdate,buyprice,sellprice,qty,t1,t2
1,2017,10,20,5,teamA,teamB
2,2017,5,10,5,teamB,teamA
Expected op:
tno,tdate,buyprice,sellprice,qty,t1,t2
1,2017,10,20,5,teamA,NaN
1,2017,10,20,5,NaN,teamB
2,2017,5,10,5,teamB,NaN
2,2017,5,10,5,NaN,TeamA
What is happening is I am separating an internal transaction between 2 different team into in 2 different transaction.
I tried using df.unstack()
and also read this answer I am missing the way to tell pandas the way I want to unstack it.
Update1:
The larger context of the problem is:
tno,tdate,buyprice,sellprice,qty,Buyerteam,Sellerteam
1,2017,10,20,5,teamA,teamB
2,2017,5,10,5,teamB,teamA
There are 2 types of trades
- Type1. where Buyerteam=NaN or Sellerteam=NaN (never both)
and I calculate
qty*(buyprice or sell price)
to calculate expenditure of team. if buyTeam is NaN, I doqty*sellprice
,if sellTeam=NaN I do qty*buyprice
. - Type2. where both t1 and t2 are not NaN (the case in this question)
I amtrying to convert type2 data into type1 data. But if I dont introduce NaN, my condition of
qty*(buyprice or sellprice)
cannot be applied
I hope my intent of introducing NaN is clear.