Iterate through list of dataframes in python

There is a list of lists of stock data. Every list consists data for a particular stock ticker. I need to find out
dfList

For each event,  I will buy with the open price on Day 1 after the crash event, then we sell it when the price increases by x=20% or decreases by y=10%. If no selling happens, then we sell it on the last day  (Day T+30) with the closing price.

Finally create a dataframe with
ticker, buying date, buying price, selling date, selling price, %price-change.

1 Like

Hello there,

Do you have a question? Is this related to your first post?: Urgent need with the python code in time series stocks data - Python - The freeCodeCamp Forum

We do not know what you need help with, unless you ask a question.

That is a different question and this is a different question. Here I need to iterate through list of dataframes. I think i can remove last post becasue I got a sort of solution.

Right, thank you, for telling.

But, what are you struggling with? Do you need help with iterating in Python? Or, do you need help with working with Dataframes?

So if you see my question,
There is a list “dfList” which contains multiple dataframes. Each dataframe has 10 columns which stores data for a particular stock. I need to iterate through the dfList and for 3rd row of every dataframe , the Open price will be my buying price. Now will check in the rest of data for the particular data if there is decrease of 10% in the buying price or increase of 20% in the buying price , which willbe our selling price. I need to do this for all the stocks.

I tried this code.

for i in range(0,len(dfList)):
  tempval = dfList[i].loc[2]['Open']
  for j in range(0,len(dfList[i])):
    if ((dfList[i].loc[j]['Open'] > (1.2 * tempval)) | (dfList[i].loc[j]['Open'] < (0.9 * tempval))):
      print(dfList[i].loc[j])
      break

but it is not working

This is how my dfList is

dfList[:2]

[    Event_index       Date    Open  ...     Volume  Name  Loss percent
 0             1 2018-02-05   830.0  ...  1324579.0  TVIX     68.000000
 1             1 2018-02-06  1489.0  ...   987706.0  TVIX    -40.793651
 2             1 2018-02-07   914.0  ...   683893.0  TVIX     21.849866
 3             1 2018-02-08   924.0  ...  1122493.0  TVIX     48.514851
 4             1 2018-02-09  1160.0  ...   949449.0  TVIX    -16.296296
 5             1 2018-02-12  1058.5  ...   545203.0  TVIX     -7.876106
 6             1 2018-02-13  1096.0  ...   325524.0  TVIX     -0.288184
 7             1 2018-02-14  1030.0  ...   432958.0  TVIX    -21.772640
 8             1 2018-02-15   756.0  ...   257326.0  TVIX     -4.187192
 9             1 2018-02-16   820.0  ...   398125.0  TVIX      2.185090
 10            1 2018-02-20   847.0  ...   282795.0  TVIX      8.553459
 11            1 2018-02-21   840.0  ...   312487.0  TVIX      2.201622
 12            1 2018-02-22   834.0  ...   280997.0  TVIX     -2.267574
 13            1 2018-02-23   819.0  ...   295986.0  TVIX    -14.617169
 14            1 2018-02-26   698.0  ...   139378.0  TVIX     -7.336957
 15            1 2018-02-27   699.0  ...   390828.0  TVIX     16.568915
 16            1 2018-02-28   750.0  ...   413212.0  TVIX      8.930818
 17            1 2018-03-01   865.0  ...   687815.0  TVIX     14.434180
 18            1 2018-03-02  1076.0  ...   672461.0  TVIX     -8.678103
 19            1 2018-03-05   937.0  ...   322371.0  TVIX     -9.060773
 20            1 2018-03-06   810.0  ...   305395.0  TVIX      1.579587
 21            1 2018-03-07   900.0  ...   418296.0  TVIX     -2.033493
 22            1 2018-03-08   797.5  ...   340665.0  TVIX     -6.471306
 23            1 2018-03-09   741.0  ...   323610.0  TVIX    -13.838120
 24            1 2018-03-12   681.0  ...   178714.0  TVIX      6.060606
 25            1 2018-03-13   681.0  ...   378357.0  TVIX      3.571429
 26            1 2018-03-14   704.0  ...   387484.0  TVIX      3.862069
 27            1 2018-03-15   733.0  ...   311277.0  TVIX     -5.710491
 28            1 2018-03-16   702.0  ...   255317.0  TVIX     -3.098592
 29            1 2018-03-19   716.0  ...   708294.0  TVIX     17.587209
 30            1 2018-03-20   800.0  ...   321277.0  TVIX     -4.079110
 
 [31 rows x 10 columns],
     Event_index       Date       Open  ...    Volume  Name  Loss percent
 0             2 2018-02-23  28.799999  ...    3656.0  ABIO      3.225805
 1             2 2018-02-26  13.500000  ...  580117.0  ABIO    -71.874998
 2             2 2018-02-27   9.540000  ...  459467.0  ABIO     42.222221
 3             2 2018-02-28  11.700000  ...  776422.0  ABIO     26.562494
 4             2 2018-03-01  16.200001  ...  255061.0  ABIO    -11.111110
 5             2 2018-03-02  12.960000  ...   95650.0  ABIO     -4.166666
 6             2 2018-03-05  12.600000  ...   86344.0  ABIO     -1.449278
 7             2 2018-03-06  12.420000  ...   64228.0  ABIO      0.000000
 8             2 2018-03-07  12.420000  ...   49217.0  ABIO      0.000000
 9             2 2018-03-08  12.060000  ...   59811.0  ABIO     -8.823529
 10            2 2018-03-09  11.520000  ...   54211.0  ABIO      4.838709
 11            2 2018-03-12  11.700000  ...   32844.0  ABIO     -3.076920
 12            2 2018-03-13  11.340000  ...   22644.0  ABIO     -3.174609
 13            2 2018-03-14  10.980000  ...  102511.0  ABIO     16.393445
 14            2 2018-03-15  12.960000  ...   44994.0  ABIO     -7.042251
 15            2 2018-03-16  11.700000  ...   18222.0  ABIO     -1.515154
 16            2 2018-03-19  11.880000  ...   62517.0  ABIO      0.000000
 17            2 2018-03-20  11.880000  ...   63794.0  ABIO     -4.615384
 18            2 2018-03-21  11.160000  ...   59850.0  ABIO      0.000000
 19            2 2018-03-22  11.340000  ...   20611.0  ABIO     -1.612906
 20            2 2018-03-23  10.980000  ...   62706.0  ABIO     -9.836065
 21            2 2018-03-26   9.900000  ...   56239.0  ABIO     -7.272721
 22            2 2018-03-27   9.540000  ...   58111.0  ABIO      1.960777
 23            2 2018-03-28   9.180000  ...   29439.0  ABIO     -5.769231
 24            2 2018-03-29   9.000000  ...   14767.0  ABIO      2.040820
 25            2 2018-04-02   9.000000  ...   39111.0  ABIO      3.999996
 26            2 2018-04-03   9.540000  ...   15883.0  ABIO     -5.769231
 27            2 2018-04-04   9.000000  ...   15444.0  ABIO      2.040820
 28            2 2018-04-05   9.000000  ...   13350.0  ABIO      2.000003
 29            2 2018-04-06   9.360000  ...   13128.0  ABIO      0.000000
 30            2 2018-04-09   9.360000  ...    7161.0  ABIO      0.000000
 
 [31 rows x 10 columns]]

Are you aware you are doing bitwise comparison here? The or operator in Python is the word or (in JavaScript ||)

I also tried this using “or” but when we print it it only printing the tempval values.

Can you help me with how to take only the first occurance which matches the condition in this situation ?