【问题标题】:Multiple df with a loop带有循环的多个df
【发布时间】:2021-01-29 02:40:03
【问题描述】:

我想用循环创建多个 df

这是我的代码:

index_investing= {"S&P 500","Nasdaq 100"}

对于 index_investing 中的 names_index:

df_names_index=investpy.get_index_historical_data(index=names_index, country='United States', from_date='01/01/2005', to_date= '31/12/2020')

代码返回df_names_index,即df_Nasdaq 100

如何创建循环:

df_S&P 500 和 df_Nasdaq 100

【问题讨论】:

  • 动态创建变量名通常是个坏主意。更好地创建以索引为键、数据帧为值的字典。
  • 你能解释一下吗?
  • 在循环之前创建一个字典,并在循环的每次迭代中添加一个新条目。
  • 我该怎么做对不起我的新手
  • 那么你应该先通过Python tutorial如果还没有完成。

标签: python dataframe loops


【解决方案1】:

这个问题的答案比看起来要简单。 使用

for names,index in index_investing

【讨论】:

  • ValueError Traceback(最近一次调用最后一次) 中的 ----> 1 用于名称,index_investing 中的索引:2 df_names_index=investpy.get_index_historical_data(index = names_index, 3 country='United States', 4 from_date='01/01/2005', 5 to_date= '31/12/2020') ValueError: 要解压的值太多(预期 2)
  • 你希望这会做什么?
猜你喜欢
  • 2016-09-16
  • 2020-11-28
  • 2017-05-03
  • 2014-06-12
  • 1970-01-01
  • 2020-10-23
  • 2019-09-13
  • 2016-07-24
  • 2016-02-03
相关资源
最近更新 更多