I have two data frames:
df1 = pd.DataFrame({'Group': ['xx', 'yy', 'zz', 'x', 'x', 'x','z','y','y','y','y'],
'Name': ['A', 'B', 'C', None, None, None, None, None, None, None, None],
'Value': [5, 3, 4, 7, 1, 3, 6, 5, 9, 5, 4]})
df2 = pd.DataFrame({'Name': ['A', 'A', 'B', 'B'],
'Group': ['x', 'y', 'z', 'y'],
'Repeat': [3, 2, 1, 2]}).
All the NaN
of df1["Name"]
have to fill by df2["Name"]
by matching "Group"
. Can repeat matching and filling by "Repeat"
times.
Desired output:
df = pd.DataFrame({'Group': ['xx', 'yy', 'zz', 'x', 'x', 'x','z','y','y','y','y'],
'Name': ['A', 'B', 'C', 'A', 'A', 'A', 'B', 'A', 'A', 'B', 'B'],
'Value': [5, 3, 4, 7, 1, 3, 6, 5, 9, 5, 4]})
Also looking for the fastest run time.
I did this
for index2, row2 in df2.iterrows():
for i in range(0, row2[2]):
for index1 in df1.index:
if df1.iloc[index1, 1] == row2[1] and df1.iloc[ndex1, 1] == 'NaN':
df1.iloc[ndex1, 1] = row2[0]
break
Looking for simpler and faster solution.