想要在循环中使用列表来改变变量的值。

huangapple go评论74阅读模式
英文:

want to change the value of variable using list in loop python

问题

以下是翻译好的代码部分:

symbols = ["NIFTY", "ITC", "BBC", ....etc]

from tvDatafeed import TvDatafeed, Interval

tv = TvDatafeed()

tv = TvDatafeed(username, password, chromedriver_path=None)

tv = TvDatafeed(auto_login=true)

nifty_data = tv.get_hist(symbol='NIFTY', exchange='NSE', interval=Interval.in_1_hour, n_bars=1)

print(nifty_index_data)
#result will be like this in pandas>>>>>>>

| datetime | symbol | open | high | low | close | volume |
| --- | --- | --- | --- | --- | --- | --- |
| 2023-07-20 08:45:00 | NSE:NIFTY | 19943.15 | 19991.85 | 19935.45 | 19987.05 | 60215122.0 |

There is around 1060 stocks/symbols and we have to run script again-again to scrap different  data for different symbols like this \>\>\>

# nifty
nifty_data = tv.get_hist(symbol='NIFTY', exchange='NSE', interval=Interval.in_1_hour, n_bars=1)

# ITC
ITC_data = tv.get_hist(symbol='ITC', exchange='NSE', interval=Interval.in_1_hour, n_bars=1)

# RELIANCE
RELIANCE_data = tv.get_hist(symbol='RELIANC', exchange='NSE', interval=Interval.in_1_hour, n_bars=1)

print(nifty_data)
print(ITC_data)
print(RELIANCE_data)

#this will return 3 different tables

#i need data in 1 datafame

需要注意的是,代码中的库和变量名没有进行中文翻译,因为您要求只翻译代码部分,不要有其他内容。

英文:
symbols = ["NIFTY", "ITC", "BBC", ....etc]
from tvDatafeed import TvDatafeed, Interval

tv = TvDatafeed()

tv = TvDatafeed(username, password, chromedriver_path=None)

tv = TvDatafeed(auto_login=true)

nifty_data = tv.get_hist(symbol='NIFTY',exchange='NSE',interval=Interval.in_1_hour,n_bars=1)

print(nifty_index_data)
#result will be like this in pandas>>>>>>>
datetime symbol open high low close volume
2023-07-20 08:45:00 NSE:NIFTY 19943.15 19991.85 19935.45 19987.05 60215122.0

There is around 1060 stocks/symbols and we have to run script again-again to scrap different data for different symbols like this >>>

# nifty
nifty_data = tv.get_hist(symbol='NIFTY',exchange='NSE',interval=Interval.in_1_hour,n_bars=1)

# ITC
ITC_data = tv.get_hist(symbol='ITC',exchange='NSE',interval=Interval.in_1_hour,n_bars=1)


# RELIANCE
RELIANCE_data = tv.get_hist(symbol='RELIANC',exchange='NSE',interval=Interval.in_1_hour,n_bars=1)

print(nifty_data)
print(ITC_data)
print(RELIANCE_data)

#this will return 3 different tables

#i need data in 1 datafame 

I need all symbol data in one data frame like this>>>

datetime symbol open high low close volume
2023-07-20 03:45:00 NSE:BORORENEW 474.50 485.00 473.45 483.75 89517.0
2023-07-20 03:45:00 NSE:NIFTY 19841.15 19844.00 19784.55 19813.35 19934954.0

AND I ALSO DONT WAN'T TO RUN SCRIPT AGAIN-AGAIN BY CHANGING SYMBOL NAME

PLESE HELP THIS IS MY FIRST INTERVIEW PRESENTATION

**LIBARRY THAT I USED>>>TVDATAFEED **

答案1

得分: 1

以下是翻译好的部分:

import pandas as pd

symbols = ["NIFTY", "ITC", "RELIANC"]

result = pd.concat([tv.get_hist(symbol=symbol, exchange='NSE', interval=Interval.in_1_hour, n_bars=1) for symbol in symbols])
英文:

Something like this should work:

import pandas as pd

symbols = ["NIFTY", "ITC", "RELIANC"]

result = pd.concat([tv.get_hist(symbol=symbol,exchange='NSE',interval=Interval.in_1_hour,n_bars=1) for symbol in symbols])


</details>



huangapple
  • 本文由 发表于 2023年7月20日 19:50:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729561.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定