0

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!

Ricardo Francois
  • 752
  • 7
  • 24
  • `df['Price_Copy'] = df.Price` – jezrael Nov 26 '20 at 05:48
  • I used a simpler example here but I can't do that because I'm doing some error checking per row (e.g.```np.isnan(df.get("Price")[i])```) Knew someone was gonna comment that and close this lmao – Ricardo Francois Nov 26 '20 at 05:56
  • Sorry, not understand. I question is write need duplicate column, do you need something else? Can you change data sample with expected output? – jezrael Nov 26 '20 at 05:59
  • Not a 100% duplicate column because I'm working with a much more complex dataset than what I've given in my example which requires me to check variable type in every row which is why I'm using a ```for``` loop for iteration. In theory, I only want certain variable types to be duplicated which is why I have the ```if``` conditions. – Ricardo Francois Nov 26 '20 at 06:02
  • Ya, problem is if reopen, you got exatly this answer `df['Price_Copy'] = df.Price` or `df = df.assign(Price_Copy = df.Price)`, so ask why it not working. – jezrael Nov 26 '20 at 06:04
  • So it is reason for ask change data with expected output, so not possible use simple `df['Price_Copy'] = df.Price` – jezrael Nov 26 '20 at 06:05
  • Yeah I didn't use the best sample dataset for this, man stackoverflow is actually ruthless – Ricardo Francois Nov 26 '20 at 06:09
  • ya, so please change it. – jezrael Nov 26 '20 at 06:09
  • I wanted duplicate values but I mentioned that I wanted the values to be "duplicated this way", implying I wanted my logic to be fixed. Guess I wasn't clear enough haha – Ricardo Francois Nov 26 '20 at 06:10
  • Ok, so need create new column only if this column exist and if exist there is at least one NaN? Or create new column if exist column `Price` ? – jezrael Nov 26 '20 at 06:12
  • Create new column regardless, Copy values only if ```Price``` exists for row and ```Price``` value for row is not NaN – Ricardo Francois Nov 26 '20 at 06:14
  • OK, so if Price value is `nan` then what should be filled in new column? NaN? 1? -1? – jezrael Nov 26 '20 at 06:17
  • If price value is nan, fill with 0 – Ricardo Francois Nov 26 '20 at 06:21
  • OK, so need `df['Price_Copy'] = np.where(df.Price.isna(), 0, df.Price)` ? Or `df['Price_Copy'] = df.Price.fillna(0)` ? – jezrael Nov 26 '20 at 06:22

0 Answers0