Python Benchmark: Grow pandas DataFrame

Looking for a fast way to "Grow pandas DataFrame" in python? Check below to see see which method is the fastest and uses less memory.

Packages

List packages as it's done in a requirements.txt file. Learn more

Setup

Runs before the benchmark, and it is not measured. Good place to import libraries and prepare data. Learn more

Code 1

Code 2

Code 3

Code 4

Teardown

Runs after the benchmark, and it is not measured. Good place to print out results. Learn more

Results

Fastest

Dictionary

2.10 ms

476 op/s

Speed

BenchmarkRuntime
Operations
VS
Visual
Dictionary2.10 ms476 op/s0.0021013558443103517
pandas.concat once159 ms6.28 op/s76×0.1591992722824216
pandas.concat329 ms3.04 op/s157×0.3288725893944502
DataFrame.append379 ms2.64 op/s180×0.378707823343575

Memory

BenchmarkPeak Memory
Score
VS
Visual
DataFrame.append312 KB5.93312
Dictionary476 KB8.001.52×476
pandas.concat once696 KB5.952.2×696
pandas.concat896 KB5.532.9×896

Output

Dictionary
      city  cost  number
0   London    32       0
1    Paris    22       1
2   Berlin    64       2
3   London    43       3
4    Paris    79       4
..     ...   ...     ...
95  Berlin    32      95
96  London    57      96
97   Paris    73      97
98  Berlin    88      98
99  London    12      99

[100 rows x 3 columns]
pandas.concat
      city cost number
0   London   62      0
0    Paris   50      1
0   Berlin   63      2
0   London   83      3
0    Paris   88      4
..     ...  ...    ...
0   Berlin   22     95
0   London   96     96
0    Paris    5     97
0   Berlin   30     98
0   London   21     99

[100 rows x 3 columns]
pandas.concat once
      city  cost  number
0   London    87       0
0    Paris    47       1
0   Berlin    71       2
0   London    81       3
0    Paris    43       4
..     ...   ...     ...
0   Berlin    21      95
0   London    15      96
0    Paris    85      97
0   Berlin    76      98
0   London    81      99

[100 rows x 3 columns]
DataFrame.append
      city cost number
0   London   81      0
0    Paris   29      1
0   Berlin   96      2
0   London   97      3
0    Paris   99      4
..     ...  ...    ...
0   Berlin   58     95
0   London    7     96
0    Paris   27     97
0   Berlin   87     98
0   London   17     99

[100 rows x 3 columns]