I'm using the method .lookup()
on two distinct dataframes in the sense df2.lookup(df1.index, df1.column)
(i.e., it's different to Pandas - select column using other column value as column name).
Consider the following MWE:
# Parameter
lo = -5
hi = 5
n = 4
idx = range(n)
rep = 2
# DF 1
idx_1 = np.tile(idx, rep)
data_1 = np.random.randint(lo, hi, n*rep)
df_1 = pd.DataFrame(data_1, index=idx_1, columns=['column'])
# DF 2
idx_2 = idx
col_2 = range(lo, hi+1)
data_2 = np.random.rand(n, len(col_2))
df_2 = pd.DataFrame(data_2, index=idx_2, columns=col_2)
# Result
result = df_2.lookup(df_1.index, df_1.column)
Which is, in my opinion, very convenient and easy to understand. Pandas tells me:
FutureWarning: The 'lookup' method is deprecated and will beremoved in a future version.You can use DataFrame.melt and DataFrame.locas a substitute.
Unfortunately, I would not know how the substitue works.
An intuitive but rather inefficient solution would be
result = [df_2.loc[df_1.index[i], df_1.iloc[i, 0]] for i in range(n*rep)]
Is there an easy to implement substitute for the task above that substitutes df.lookup()
via built-ins?