'pandas dataframe.at for a whole series
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) | .. |
| ... | ... | ... |
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | PlaidMode |
