-1

I have a dataframe as follows:

    location   |   amount
    ---------------------------
1   new york          $27.00
2   california        $21.00
3   florida           $19.00
4   texas             $18.00

What I want to do is split the row where Location='California' into two rows where California turns into 'Sacramento' and 'Los Angeles' and the amount (21) gets divided into two, split between the two new rows.

This is the desired result:

    location   |   amount
------------------------------
1   new york          $27.00
2   los angeles       $10.50
3   sacramento        $10.50
4   florida           $19
5   texas             $18
cdlabs45
  • 107
  • 2
  • 8

1 Answers1

0

Duplicating & Removing

cal = df.loc["location" == "california"]

df = df.append({
     "location": "sacramento",
     "amount":  cali["amount"] / 2
      }, ignore_index=True)

df = df.append({
     "location": "los angeles",
     "amount":  cali["amount"] / 2
      }, ignore_index=True)

df.drop(cal.index.to(list))

Sources: https://www.codeforests.com/2020/09/27/pandas-split-one-row-of-data-into-multiple-rows/

Python pandas: fill a dataframe row by row

Larry the Llama
  • 958
  • 3
  • 13