简体   繁体   中英

is there way to extract data from dictionary to csv file

Below code will get multiple stocks data into dictionary. But I need them into a single csv file

Code:

import pandas as pd
from datetime import datetime as dt
from nsepy import get_history

stocks = ['JSWSTEEL','RELIANCE','AXISBANK','HCLTECH','TECHM']
df = {}
for tickers in stocks:
    df[tickers] = get_history(tickers,dt(2020,1,1),dt(2021,1,1))

Use pandas to create a DataFrame out of the dict and then pandas.to_csv() function like this:

import pandas as pd
from datetime import datetime as dt
from nsepy import get_history

stocks = ['JSWSTEEL','RELIANCE','AXISBANK','HCLTECH','TECHM']
df = {}
for tickers in stocks:
    df[tickers] = get_history(tickers,dt(2020,1,1),dt(2021,1,1))

dataset = pd.DataFrame([df], index=[0])
csv_data = dataset.to_csv("your_file_path.csv")

Works beautifully!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM