So I am trying to add a new column to my dataframe that contains the side/radius given the shape and area of each row.
My original dataset looks like this:
df:
shape color area
0 square yellow 9409.0
1 circle yellow 4071.5
2 triangle blue 2028.0
3 square blue 3025.0
But when I coded it like this:
df['side'] = 0
for x in df['shape']:
if x == 'square':
df['side'] = np.rint(np.sqrt(df['area'])).astype(int)
elif x == 'triangle':
df['side'] = np.rint(np.sqrt((4 * df['area'])/np.sqrt(3))).astype(int)
elif x == 'circle':
df['side'] = np.rint(np.sqrt(df['area']/np.pi)).astype(int)
I got:
shape color area size
0 square yellow 9409.0 55
1 circle yellow 4071.5 36
2 triangle blue 2028.0 25
3 square blue 3025.0 31
It looks like the loop is adding the elif x == 'circle' clause to the side column for every row.
forloop.df['shape']