from pandas import DataFrame
import time
data = []
for i in range(3000):
data.append(['SH601318', 'abcdef', 0.0001215, 0.000215, 0.125, 0.243])
df = DataFrame(data)
df.columns = ['symbol', 'name', 'total_ratio', 'outstanding_ratio', 'avg_total_ratio', 'avg_outstanding_ratio']
t = time.time()
result = [{
'symbol': df.at[i, 'symbol'],
'name': df.at[i, 'name'],
'total_ratio': df.at[i, 'total_ratio'],
'outstanding_ratio': df.at[i, 'outstanding_ratio'],
'avg_total_ratio': df.at[i, 'avg_total_ratio'],
'avg_outstanding_ratio': df.at[i, 'avg_outstanding_ratio'],
} for i in range(len(df))]
print '%.2f seconds' % (time.time() - t)
# 0.25 seconds
t = time.time()
result = [df.iloc[i].to_dict() for i in range(len(df))]
print '%.2f seconds' % (time.time() - t)
# 0.58 seconds
I tried 2 ways to convert DataFrame to list of dict. But both are very slow, 250 ms and 580 ms! That's far more than time I query from database. I don't know why it takes so much time, after all, manipulating memory is quicker than disk. I expected this time is in 10 ms. Is there any way I can achieve it?