简体   繁体   中英

Pandas append is very slow, having problems using from_dict

I need to append pandas dataframes. But I found out that append is very slow. This link Improve Row Append Performance On Pandas DataFrames suggests using from_dict instead of append. I tried to do the same but I am having a problem, converting a dataframe to_dict and back from dict to dataframe.

I have this dataframe df

        date   open   high    low  close  volume  average  barCount simb
0  3/31/2020  81.43  81.49  78.56  78.91  183417  80.0940     86742  xdt
1   4/1/2020  77.00  77.38  75.35  76.57   91420  76.4395     49399  xdt
2   4/2/2020  76.12  79.66  76.00  79.44   75298  78.4080     40614  xdt
3   4/3/2020  78.79  79.99  78.18  79.45   64965  79.0490     37140  xdt
4   4/6/2020  81.08  83.12  79.60  82.73   89395  81.3605     46247  xdt
5   4/7/2020  83.45  84.48  81.76  81.93   77722  83.3980     43947  xdt
6   4/8/2020  82.50  85.39  81.05  84.95   66202  83.4955     40256  xdt
7   4/9/2020  85.00  86.50  82.95  86.04   80298  85.1100     46184  xdt
8  4/13/2020  86.32  86.48  83.52  85.85   48114  85.1790     27280  xdt
9  4/14/2020  87.00  89.54  86.50  89.14   75528  88.4410     42810  xdt

I have several thousands of dataframes like this. I need to convert them to dict and then all of them to back to dataframe as it is shown on the link.

My code

d = {} 
i=0
d[i] =df.to_dict( 'index')
pd.DataFrame.from_dict(d, 'index')

I cannot get a proper dataframe with this code. I used different options instead of 'index' option, but it did not help. I would appreciate it if someone could help me with the code

Read all the dataframes into a list

from glob import glob

dfs = [pd.read_csv(file) for file in glob('*.csv')]

Then use pd.concat

big_df = pd.concat(dfs)

NOTE: glob('*.csv') reads all csv files in the current working directory.

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