我有一个名为final_result的列表列表,其结构如下:
[[Pandas Dataframe,int],[Pandas Dataframe,int],[Pandas Dataframe,int],...]
当我跑步时:
for i in final_result:
print(final_result.index(i))
if int(final_result.index(i)) != 0:
print("got here")
else: print("got here2")
我得到以下回溯:
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
但是,正确的索引,0和“ got here2”实际上是在功能中断之前打印到控制台的。我尝试删除了print命令,然后在if语句上将其中断(因此,不会将0打印到控制台,但是“ got here2”仍然可以)。这令人惊讶,因为它似乎贯穿该函数一次,直到在print语句处中断。在这种列表列表中检索第一层列表的索引的正确方法是什么?
样本数据:
[[ 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]]
预期输出:
0
got here2
1
got here
2
got here
3
got here
4
got here
答案 0 :(得分:0)
ValueError ,通常是指请求不在定义的预期范围内的项目。
例如,我有两个项目[1,3]的列表,但是当我检查索引4处的项目时,出现“值错误”。项目1
的索引为0,项目3
的索引为1
。
为避免这种情况,我们首先可以简化解析
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")
有关代码的解决方案:
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")