1

How can I convert the following code without using the loop, using pythonic pandas :

import pandas as pd

xls = pd.ExcelFile('...')
df_1 = pd.read_excel(xls, '...')
df_2 = pd.read_excel(xls, '...')

df_1_len = len(df_1)
results = np.empty(df_1_len)
for idx in range(df_1_len):
    results[idx] = df_2.at[df_1['x'][idx], df_1['y'][idx]]

df_1 looks like :

x y
1 a
2 a
2 b
... ...

df_2 looks like :

1 2 ..
a val(1,a) val(2,a) ..
b val(1,n) val(2,a) ..
... ... ...
mlisthenewcool
  • 539
  • 3
  • 14

1 Answers1

0

did you try df_2.loc[df_1['x'], df_1['y']]? Maybe this answer can help you understanding the difference between at and loc.

PlaidMode
  • 37
  • 6