I am attempting to select a row of data, merge it with another dataframe, then select the next row, performing the same operation. I believe I should be able to do this with pandas.iterrows.
import pandas as pd
import numpy as np
one = {'A' : pd.Series([3, 4, 2]), 'B' : pd.Series(['one', 'two', 'one'])}
two = {'A' : pd.Series([1, 2, 3, 4, 5, 6])}
df1 = pd.DataFrame(one, columns = ['A', 'B'])
df2 = pd.DataFrame(two)
for index, row in df1.iterrows():
row1 = pd.DataFrame(row)
print(row1)
df3 = pd.merge(df2, row1, on = 'A', how = 'left')
print (df3)
When I print(row1), I get:
0
A 3
B one
1
A 4
B two
2
A 2
B one
The join fails due to a key error, which makes sense to me given the structure of print(row1).
The desired outcome of df3 is:
A B
0 1 Nan
1 2 Nan
2 3 one
3 4 Nan
4 5 Nan
5 6 Nan
A B
0 1 Nan
1 2 Nan
2 3 Nan
3 4 two
4 5 Nan
5 6 Nan
It appears to me that the column labels are now the index. I think I need to reset the index, so that 'A' and 'B' will be values that I can join on. Is there an efficient way to accomplish this?