| 1 |
import pandas as pd |
| 2 |
import numpy as np |
| 3 |
import timeit |
| 4 |
|
| 5 |
np.random.seed(1) |
| 6 |
|
| 7 |
column_names_example = [i for i in range(10000)] |
| 8 |
index = pd.MultiIndex.from_tuples( |
| 9 |
[("left", c) for c in column_names_example] |
| 10 |
+ [("right", c) for c in column_names_example] |
| 11 |
) |
| 12 |
df = pd.DataFrame(np.random.rand(1000, 20000), columns=index) |
| 13 |
|
| 14 |
|
| 15 |
def keep_column(left_col, right_col): |
| 16 |
return ( |
| 17 |
left_col[left_col.first_valid_index()] > right_col[right_col.last_valid_index()] |
| 18 |
) |
| 19 |
|
| 20 |
|
| 21 |
def do_it_original(): |
| 22 |
v = [c for c in column_names_example if keep_column(df["left"][c], df["right"][c])] |
| 23 |
return v |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
def do_it(): |
| 28 |
left_cols = df["left"].loc[:, column_names_example] |
| 29 |
right_cols = df["right"].loc[:, column_names_example] |
| 30 |
v = left_cols.columns[left_cols.iloc[0] > right_cols.iloc[-1]] |
| 31 |
return v |
| 32 |
|
| 33 |
|
| 34 |
do_it() |