0

I'm working with a dataset that has more than 40 columns and I want to replace two columns data with another two columns of data and want to remove the existing one.

Example Dataset:

v_4        v5             s_5     vt_5     ex_5          pfv           pfv_cat
0-50      StoreSale     Clothes   8-Apr   above 100   FatimaStore       Shoes
0-50      StoreSale     Clothes   8-Apr   0-50        DiscountWorld     Clothes
51-100    CleanShop     Clothes   4-Dec   51-100      BetterUncle       Shoes

I want to replace the v5 column with pfv and s_51 withpfv_cat, by replace I meanoverwrite`

Here's what I have tried:

df.replace(df['v_5'].tolist(), df['pfv'].tolist())
df.replace(df['s_5'].tolist(), df['pfv_cat'].tolist())

But it doesn't work, it's just stuck and no input.

1 Answer 1

1

You can just do

df['v_5']=df['pfv']
df['s_5']=df['pfv_cat']
print(df)

Input

     v_4          v5    s_5         vt_5    ex_5             pfv        pfv_cat
0   0-50    StoreSale   Clothes     8-Apr   above100    FatimaStore     Shoes
1   0-50    StoreSale   Clothes     8-Apr   0-50        DiscountWorld   Clothes
2   51-100  CleanShop   Clothes     4-Dec   51-100      BetterUncle     Shoes

Output

v_4     v5  s_5     vt_5    ex_5    pfv     pfv_cat     v_5
0   0-50    StoreSale   Shoes   8-Apr   above100    FatimaStore     Shoes   FatimaStore
1   0-50    StoreSale   Clothes     8-Apr   0-50    DiscountWorld   Clothes     DiscountWorld
2   51-100  CleanShop   Shoes   4-Dec   51-100  BetterUncle     Shoes   BetterUncle
Sign up to request clarification or add additional context in comments.

2 Comments

for this code it's just stuck and no input ... still the same
you need to print(df), to see the changes. added that to the code now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.