Python 遍历行,运行 并保存

Python Iterate through rows, run and save

我有一个 pandas 数据框,我想在其中循环遍历它的行,运行 并保存输出,如果有任何错误则忽略它并移至下一行。

import pandas as pd
from nsepy import get_history #can be install by "pip install nsepy"
from datetime import date

data = {'script': ['SBIN = get_history(symbol="SBIN", start=date(1985,1,1), end=date(2022,1,31))',
'SAIL = get_history(symbol="SAIL", start=date(1985,1,1), end=date(2022,1,31))', 
'20MICRONS = get_history(symbol="20MICRONS", start=date(1985,1,1), end=date(2022,1,31))',
'RELIANCE = get_history(symbol="RELIANCE", start=date(1985,1,1), end=date(2022,1,31))']}  

df = pd.DataFrame(data)  

现在我想运行每一行一行一行 我可以做到

#run each row
#1
SBIN = get_history(symbol="SBIN", start=date(1985,1,1), end=date(2022,1,31))
df1.to_csv('SBIN', sep="\t")
#2
SAIL = get_history(symbol="SAIL", start=date(1985,1,1), end=date(2022,1,31))'
df1.to_csv('SAIL', sep="\t")
#3
20MICRONS = get_history(symbol="20MICRONS", start=date(1985,1,1), end=date(2022,1,31))
df1.to_csv('20MICRONS', sep="\t")
#4
RELIANCE = get_history(symbol="RELIANCE", start=date(1985,1,1), end=date(2022,1,31))
df1.to_csv('RELIANCE', sep="\t")

但这需要很长时间。那么如何通过for循环或while循环来完成

请注意,我想 运行 每行并将输出保存为在同一行的 = 符号之前提取的字符,例如第一行的“SBIN”。如果任何一行有任何错误,则忽略该错误并移至下一行(第 3 行将 return 由于数据不可用而导致的错误)

由于您的进程是IO-Bounded,您可以使用Threading来提高速度。 你可以试试这个:

import pandas as pd
from nsepy import get_history
from datetime import date
import concurrent.futures

history = {
    "SBIN": {"start": date(2021, 1, 1), "end": date(2022, 1, 31)},
    "SAIL": {"start": date(2021, 1, 1), "end": date(2022, 1, 31)},
    "20MICRONS": {"start": date(2021, 1, 1), "end": date(2022, 1, 31)},
    "RELIANCE": {"start": date(2021, 1, 1), "end": date(2022, 1, 31)},
}


def get_historical_data(symbol, /, **kwds):
    print(symbol)
    df = get_history(symbol, **kwds)
    df.to_csv(f'{symbol}.csv', sep='\t')
    return df


data = []
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
    future_history = [
        executor.submit(get_historical_data, symbol, **data)
        for symbol, data in history.items()
    ]

    data = []
    for future in concurrent.futures.as_completed(future_history):
        data.append(future.result())
    df = pd.concat(data)