I have df1
df1 = pd.DataFrame({'x':[1,2,3,5],
'y':[2,3,4,6],
'value':[1.5,2.0,0.5,3.0]})
df1
x y value
0 1 2 1.5
1 2 3 2.0
2 3 4 0.5
3 5 6 3.0
and I want to assign the value
at x
and y
coordinates to another dataframe df2
df2 = pd.DataFrame(0.0, index=[x for x in range(0,df1['x'].max()+1)], columns=[y for y in range(0,df1['y'].max()+1)])
df2
0 1 2 3 4 5 6
0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
1 0.0 0.0 0.0 0.0 0.0 0.0 0.0
2 0.0 0.0 0.0 0.0 0.0 0.0 0.0
3 0.0 0.0 0.0 0.0 0.0 0.0 0.0
4 0.0 0.0 0.0 0.0 0.0 0.0 0.0
5 0.0 0.0 0.0 0.0 0.0 0.0 0.0
by
for x, y, value in zip(df1['x'],df1['y'],df1['value']):
df2.at[x,y] = value
to give
0 1 2 3 4 5 6
0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
1 0.0 0.0 1.5 0.0 0.0 0.0 0.0
2 0.0 0.0 0.0 2.0 0.0 0.0 0.0
3 0.0 0.0 0.0 0.0 0.5 0.0 0.0
4 0.0 0.0 0.0 0.0 0.0 0.0 0.0
5 0.0 0.0 0.0 0.0 0.0 0.0 3.0
However, it is a bit slow because I have a long df1
.
Do we have a faster method than df.at[x,y]
?