Include Variable as Index Column in For Loop
Include Variable as Index Column in For Loop
Loading saved threads...
Sioux Gougui · External communityPost link
External question — Stack Overflow Stack Exchange
Author: Sioux Gougui
Original post: https://stackoverflow.com/questions/77380392
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
I'm trying to add a variable from a list into a dataframe that is being passed through an appended list: The goal is to be able to identify the quotes with their respective symbol pairs:
symbol = ['EURUSD','USDJPY','USDCHF','USDCAD']
timeframe = mt.TIMEFRAME_M1
date_from = datetime(2023,10,27,16,55)
date_to = datetime.now()
price = []
for i in symbol:
data = mt.copy_rates_range(i,timeframe,date_from,date_to)
price.append(data['close'])
prices = pd.DataFrame(price)
prices
THE CURRENT RESULTS ARE:
0 1 2 3
0 1.05644 1.05654 1.05644 1.05640
1 149.63200 149.60200 149.58400 149.60400
2 0.90209 0.90217 0.90219 0.90238
3 1.38740 1.38726 1.38732 1.38729
THE DESIRED RESULTS ARE:
PAIR 0 1 2 3
0 EURUSD 1.05644 1.05654 1.05644 1.05640
1 USDJPY 149.63200 149.60200 149.58400 149.60400
2 USDCHF 0.90209 0.90217 0.90219 0.90238
3 USDCAD 1.38740 1.38726 1.38732 1.38729
I've tried concat and using "+"
Tried this too:
https://stackoverflow.com/questions/68010466/make-a-dataframe-column-with-names-of-variables-in-the-for-loop
Quote
Report
Mark SdS · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: Mark SdS
Original post: https://stackoverflow.com/a/77382411
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
Have you tried just to start each appending with
symbol
like this:
for i in symbol:
price.append(symbol[i])
data = mt.copy_rates_range(i,timeframe,date_from,date_to)
price.append(data['close'])
...
Quote
Report
Post Reply
Quoted from Forex.com.bd-Editorial External answer — Stack Overflow Stack Exchange Author: Mark SdS Source score (net votes, not local likes): 0 Original post: https://stackoverflow.com/a/77382411 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. Have you tried just to start each appending with symbol like this: for i in symbol: price.append(symbol[i]) data = mt.copy_rates_range(i,timeframe,date_from,date_to) price.append(data['close']) ...
Checking account access…