I wanted to add a new column to a pandas df while iterating:
for index, row in df.iterrows():
row["newcolumn"] = row["oldcolumn"].normalize() #normalize() is a custom function
This, however, leaves my df unchnged. Why is this?
I wanted to add a new column to a pandas df while iterating:
for index, row in df.iterrows():
row["newcolumn"] = row["oldcolumn"].normalize() #normalize() is a custom function
This, however, leaves my df unchnged. Why is this?
Use loc
with df
:
for index, row in df.iterrows():
df.loc[index, "newcolumn"] = row["oldcolumn"].normalize()
But for better performance is better use apply
if does not exist some vectorized solution:
df["newcolumn"] = df["oldcolumn"].apply(normalize)