Home > Software engineering >  Pandas Multiply Rows and Strip the Text At Once
Pandas Multiply Rows and Strip the Text At Once

Time:09-09

I have the following Pandas dataframe where in sold column, some of the data have "RB " (in english means thousand). So, in that column, if it contains "RB ", I would like to multiply it by 1000 then delete "RB " at the same time.

Anyone knows how should I write the conditional code?

name    price   sold    rate    city    specification
0   StarGRAVIDA BHARATA OBAT KANKER PAYUDARA AMPUH...   275000  10RB    4.9 KOTA JAKARTA PUSAT  MerekBharataNegara AsalIndonesiaBerat Produk10...
1   Star Obat Herbal Kanker Payudara, Serviks, Hat...   525000  3,8RB   4.9 KOTA JAKARTA PUSAT  Masa Penyimpanan12 BulanNo. Izin Edar (BPOM, l...
2   StarObat Kanker Tumor Kista Miom Kelenjar Tiro...   184000  4,7RB   4.9 KOTA JAKARTA PUSAT  No. Izin Edar (BPOM, lainnya) Aturan Pakai Kon...
3   Walatra Zedoril 7 Asli Obat Herbal Kanker Tumo...   255000  10RB    4.8 KOTA JAKARTA PUSAT  MerekWalatraBerat Produk100gTipe PaketPaket He...
4   Obat Herbal Kanker Payudara Obat Kanker servik...   70000   2,8RB   4.8 KOTA JAKARTA PUSAT  Tipe PaketBundleNegara AsalIndonesiaMasa Penyi...
... ... ... ... ... ... ...
2496    Chang Sheuw Tian Ran Ling Shen Yao /Daun Terat...   398000  1   5.0 KOTA JAKARTA PUSAT  Stok149Dikirim DariKOTA SURABAYA
2497    StarChitin Chitosan untuk PELANGSING BADAN, MA...   458999  56  5.0 KOTA JAKARTA PUSAT  MerekTiensBerat Produk100gMasa Penyimpanan24 B...
2498    HEALTHYBEST Obat Kanker payudara penghancur be...   195000  290 5.0 KOTA JAKARTA PUSAT  MerekHealthybestBerat Produk50gTipe PaketPaket...
2499    Dekapro Bharata Asli Obat Liver Hepatitis & Ka...   150000  19  5.0 KOTA JAKARTA PUSAT  MerekDekapro BharataNo. Izin Edar (BPOM, lainn...
2500    Gravida Bharata Obat Kanker Paling Ampuh Obat ...   195000  28  5.0 KOTA JAKARTA PUSAT  MerekGravida BharataNo. Izin Edar (BPOM, lainn...

CodePudding user response:

Try this in your dataframe.

Replace comma with dot

Replace RB with 1000 multiplication

Replace anything that is not numbers, dot or multiplication

Evaluate the expression

df['sold'] = (df.sold.str.replace(",", ".")
                  .str.replace("RB", "*1000")
                  .str.replace("[^0-9.*]",'')
                  .apply(eval)
              )
  • Related