簡體   English   中英

Python 遍歷行,運行並保存

[英]Python Iterate through rows, run and save

我有一個 pandas dataframe,我想在其中循環遍歷它的行,運行並保存 output,如果有任何錯誤,則忽略它並移至下一行。

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循環來完成

請注意,我想運行每一行並將 output 保存為在同一行的 = 符號之前提取的字符,例如第一行的“SBIN”。 如果任何一行有任何錯誤,則忽略該錯誤並移至下一行(第 3 行將返回由於數據不可用而導致的錯誤)

由於您的進程是 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM