0

I have a list of lists called final_result, with the following structure:

[[Pandas Dataframe, int], [Pandas Dataframe, int], [Pandas Dataframe, int], ...]

When I run:

for i in final_result:
  print(final_result.index(i))
  if int(final_result.index(i)) != 0:
    print("got here")
  else: print("got here2")

I get the following traceback:

Traceback (most recent call last):
  File "backtest.py", line 471, in backtest_mt
    print(final_result.index(i))
  File "C:\Users\eyese\.julia\conda\3\lib\site-packages\pandas\core\ops\__init__.py", line 839, in f
    "Can only compare identically-labeled DataFrame objects"
ValueError: Can only compare identically-labeled DataFrame objects

Yet, the correct index, 0, and "got here2" actually gets printed to the console prior to the function breaking. I tried removing the print command, and it then breaks on the if statement (and consequently 0 does not get printed to the console, but "got here2" still does). This is surpising, as it seems to run through the function once until breaking at the print statement. What would be the correct way to retrieve the index of a first-layer list within this kind of list of lists?

Sample data:

[[                     timestamp  capital
0    2020-05-25 17:14:00+00:00     1000
1    2020-05-25 17:15:00+00:00     1000
2    2020-05-25 17:16:00+00:00     1000
3    2020-05-25 17:17:00+00:00     1000
4    2020-05-25 17:18:00+00:00     1000
..                         ...      ...
957  2020-05-26 09:12:00+00:00  999.925
958  2020-05-26 09:13:00+00:00  999.925
959  2020-05-26 09:14:00+00:00  999.925
960  2020-05-26 09:15:00+00:00   1000.2
961  2020-05-26 09:16:00+00:00   1000.2

[962 rows x 2 columns], 1000.1964074198233], [                      timestamp  capital
0     2020-05-26 09:16:00+00:00     1000
1     2020-05-26 09:17:00+00:00     1000
2     2020-05-26 09:18:00+00:00     1000
3     2020-05-26 09:19:00+00:00     1000
4     2020-05-26 09:20:00+00:00     1000
...                         ...      ...
1887  2020-05-27 16:45:00+00:00  1001.03
1888  2020-05-27 16:46:00+00:00  1001.03
1889  2020-05-27 16:47:00+00:00  1001.03
1890  2020-05-27 16:48:00+00:00  1000.02
1891  2020-05-27 16:49:00+00:00  1000.02

[1892 rows x 2 columns], 1000.0247878589546], [                      timestamp  capital
0     2020-06-02 22:08:00+00:00     1000
1     2020-06-02 22:09:00+00:00     1000
2     2020-06-02 22:10:00+00:00  999.925
3     2020-06-02 22:11:00+00:00  999.925
4     2020-06-02 22:12:00+00:00  999.925
...                         ...      ...
2593  2020-06-04 17:23:00+00:00    999.7
2594  2020-06-04 17:24:00+00:00    999.7
2595  2020-06-04 17:25:00+00:00    999.7
2596  2020-06-04 17:26:00+00:00    999.7
2597  2020-06-04 17:27:00+00:00    999.7

[2598 rows x 2 columns], 999.6999999999999], [                      timestamp  capital
0     2020-05-27 16:49:00+00:00     1000
1     2020-05-27 16:50:00+00:00     1000
2     2020-05-27 16:51:00+00:00     1000
3     2020-05-27 16:52:00+00:00     1000
4     2020-05-27 16:53:00+00:00     1000
...                         ...      ...
3765  2020-05-30 07:38:00+00:00  1000.56
3766  2020-05-30 07:39:00+00:00  1000.56
3767  2020-05-30 07:40:00+00:00  1000.56
3768  2020-05-30 07:41:00+00:00  1001.55
3769  2020-05-30 07:42:00+00:00  1001.55

[3770 rows x 2 columns], 1001.5513098187641], [                      timestamp  capital
0     2020-05-30 07:42:00+00:00     1000
1     2020-05-30 07:43:00+00:00     1000
2     2020-05-30 07:44:00+00:00     1000
3     2020-05-30 07:45:00+00:00     1000
4     2020-05-30 07:46:00+00:00     1000
...                         ...      ...
5177  2020-06-02 22:04:00+00:00  998.021
5178  2020-06-02 22:05:00+00:00  998.021
5179  2020-06-02 22:06:00+00:00  998.021
5180  2020-06-02 22:07:00+00:00  1003.59
5181  2020-06-02 22:08:00+00:00  1003.59

[5182 rows x 2 columns], 1003.5854964926011]]

Expected output:

0
got here2
1
got here
2
got here
3
got here
4
got here

1 Answers1

0

ValueError, generally refers to asking for an item that is not within the expected boundaries defined.

For example, I have a list of two items [1, 3] but when I check for the item at index 4, I get Value Error. Item 1 is at index 0 and item 3 is at index 1.

To avoid this, we can first simplify the parsing

for each_item in list_of_list:             # We will be reading each list as item
    item_dataframe, item_int = each_item # Tuple Unpacking - Python feature 
    if item_int == 0:
        # you your stuff here
        # print(item_dataframe)
        print("got here")
    else:
        print("got here2")

Solution for code in question:

for i in range(len(final_result)):
  print(i)
  #access elements
  capital_df = final_result[i][0]
  capital_int = final_result[i][1]
  if i = 0:
    print("got here")
  else: print("got here2")
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
sam
  • 1,819
  • 1
  • 18
  • 30
  • Thank you for the answer. That works for separating the elements of the list, but I want the if statement to check if the index of the list within the list (each_item) is a certain value or not (in this case 0). When I unpack each_item with `item_dataframe, item_int = each_item`, `if list_of_list.index(each_item) != 0` still returns `ValueError: Can only compare identically-labeled DataFrame objects` – Eyesea Evan Jun 14 '20 at 06:58
  • In your code, you can change `for i in final_result:` to `for i in range(len(final_result)): ` to stop this Value error from happening! – sam Jun 14 '20 at 07:07
  • The second point, I see that second no. is a float. Comparing floats in computer systems is a challenge. Please go through https://stackoverflow.com/questions/10037115/comparing-a-float-and-an-int-in-python – sam Jun 14 '20 at 07:09
  • I could, but I do also need the elements of the list for logic which I haven't included in my post. Is it good practice to then call these elements by `final_result[i][0]`, and `final_result[i][1]`? It's strange that Python won't let me find the index of a list within a list if it contains a both a dataframe and a different type. – Eyesea Evan Jun 14 '20 at 07:13
  • Yes you can use `final_result[i][0]`. Python list is indexed. How? Internally its an array of arrays for efficiency reasons. Said that, python can accept anything! Yes anything(fns/int/string/float/,...) which makes it awesome. Check about Python data types and list-comprehensions. It makes life easy for python developers. – sam Jun 14 '20 at 07:21