遍历列并同时存储不同列的值

时间:2019-10-18 07:37:01

标签: python pandas

遍历4列并提取值,当填充了“购买数量和购买价格”(342,21)列时,应提取该行,然后检查下一行,再次从哪里获取“购买数量和购买价格”(2330,45) ),但是在检查下一行是nan时,我想提取卖出数量和卖出价格(110,56)

I Have the below Dataframe 
I wanted to extract values of 4 columns row by row
Quantity bought=342,21
Date=20.3.2019
Quantity Bought=2330,45
Date=21.3.2019
then it will fetch the other columns value
Quantity Sold=110,56
Date=22.3.2019
then again extracted are 
Quantity Sold=221,21
Date=24.3.2019
then 
Quantity Bought=344,22
Date=25.5.2019
then 
Quantity Sold=455,29
29.6.2019 
Quantity Sold=566,30
Date=30.6.2019


ef={'Date':['20.3.2019','21.3.2019','22.3.2019','24.3.2019','25.5.2019','29.6.2019','30.6.2019'],
    'Quantity Bought':[342.0,2330.0,'nan','nan',344,'nan','nan'],
    'Buy Price':[21.0,45.0,'nan','nan',22,'nan','nan'],
    'Sell Quantity':['nan','nan',110,221,'nan',455,566],
    'Sell Price':['nan','nan',56,21,'nan',29,30]}
ef=pd.DataFrame(ef)


#And I wanted to be  added simultaneously in

el=[Trade(pd.to_datetime('20.03.2019',format='%d.%m.%Y'), 342, 21)]                                                                       
el = [Trade(pd.to_datetime('21.03.2019',format='%d.%m.%Y'), 2330, 45)
el.append(Trade(pd.to_datetime('22.03.2016',format='%d.%m.%Y'), -110, 56))
el.append(Trade(pd.to_datetime('24.03.2016',format='%d.%m.%Y'), -221, 21))
el.append(Trade(pd.to_datetime('25.05.2016',format='%d.%m.%Y'), 344, 22)) 
el.append(Trade(pd.to_datetime('29.06.2016',format='%d.%m.%Y'), -455, 29))
el.append(Trade(pd.to_datetime('30.06.2016',format='%d.%m.%Y'), -566, 30))]

1 个答案:

答案 0 :(得分:1)

我希望这就是你的意思:

Android