I have a dataframe that looks something like this called df
(as a minimum reproducible example):
Brand Price
0 Honda 22000
1 Toyota 25000
2 Ford 27000
3 Audi 35000
Essentially, I just want to duplicate the "Price" values and add that in another column, so that I'll have an output that'll look something like this:
Brand Price Price_Copy
0 Honda 22000 22000
1 Toyota 25000 25000
2 Ford 27000 27000
3 Audi 35000 35000
My code and what I've tried:
for i in range(0, len(df)):
df["Price_Copy"] = np.nan
if "Price" in df.columns and not np.isnan(df.get("Price")[i]):
df.at[i, "Price_Copy"] = int(df.get("Price")[i]) if isinstance(df.get("Price")[i], np.integer)
else np.nan
My resulting dataframe only updates the last value and looks something like this and doesn't keep the Price as an Integer (but that's another problem):
Brand Price Price_Copy
0 Honda 22000 NaN
1 Toyota 25000 NaN
2 Ford 27000 NaN
3 Audi 35000 35000.0
I'm wondering how I'd be able to update all the values in the Price_Copy column to be a duplicate of the values in Price this way. Thanks in advance!