簡體   English   中英

在 Pandas 中使用 TQDM 進度條

[英]Use TQDM Progress Bar with Pandas

使用 Pandas 導入和索引大型數據集時是否可以使用 TQDM 進度條?

這是我正在導入、索引和使用 to_datetime 的一些 5 分鍾數據的示例。 這需要一段時間,如果能看到進度條就好了。

#Import csv files into a Pandas dataframes and convert to Pandas datetime and set to index

eurusd_ask = pd.read_csv('EURUSD_Candlestick_5_m_ASK_01.01.2012-05.08.2017.csv')
eurusd_ask.index = pd.to_datetime(eurusd_ask.pop('Gmt time'))

通過形狀求長度

for index, row in tqdm(df.iterrows(), total=df.shape[0]):
   print("index",index)
   print("row",row)
with tqdm(total=Df.shape[0]) as pbar:    
    for index, row in Df.iterrows():
        pbar.update(1)
        ...

tqdm > 4.24 有一個解決方法。 根據https://github.com/tqdm/tqdm#pandas-integration

from tqdm import tqdm
        
# Register `pandas.progress_apply` and `pandas.Series.map_apply` with `tqdm`
# (can use `tqdm_gui`, `tqdm_notebook`, optional kwargs, etc.)
tqdm.pandas(desc="my bar!")
eurusd_ask['t_stamp'] = eurusd_ask['Gmt time'].progress_apply(lambda x: pd.Timestamp)
eurusd_ask.set_index(['t_stamp'], inplace=True)

您可以通過正常讀取文件逐行填充 Pandas 數據幀,然后簡單地將每個新行作為新行添加到數據幀中,盡管這比僅使用 Pandas 自己的讀取方法要慢一些。

我發現它很容易實現。 您只需要添加 total 參數。

import pandas as pd
df = pd.read_excel(PATH_TO_FILE)


for index, row in tqdm(df.iterrows(),  total=df.shape[0], desc=f'Reading DF'):
        print(row(['df_colum'])

暫無
暫無

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

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