1

I have a dataframe - see below. I would like to multiply 30 by .115 and create a new column (second image). I started writing the code below but Im not sure if Im even on the right track.

DF.loc[(DF['PRO_CHARGE']=="(1.0, 99283.0)")), '%'] = DF.loc(DF['PRO_CHARGE']=="(0.0, 99283.0)")....?

enter image description here

enter image description here

Raven
  • 849
  • 6
  • 17
  • Does this answer your question? [Python: Pandas Dataframe how to multiply entire column with a scalar](https://stackoverflow.com/questions/33768122/python-pandas-dataframe-how-to-multiply-entire-column-with-a-scalar) – UpmostScarab Feb 17 '23 at 21:07
  • In creating a new column you need to define values for every row, even if this is a value of NaN. It is not clear if this is what you intend. – user19077881 Feb 17 '23 at 21:17

1 Answers1

1

With the following toy dataframe:

import pandas as pd


df = pd.DataFrame(
    {
        "PRO_CHARGE": [(0.0, 99283.0), (1.0, 99283.0), (1.0, 99284.0)],
        "TOTAL": [15, 30, 23],
        "PERC_99282": [0.115, 0.49, 0.53],
    }
)
print(df)
# Output

       PRO_CHARGE  TOTAL  PERC_99282
0  (0.0, 99283.0)     15       0.115
1  (1.0, 99283.0)     30       0.490
2  (1.0, 99284.0)     23       0.530

Here is one way to it with Pandas .at property, which provides fast setting (as opposed to .loc) for a single value:

df.at[df["PRO_CHARGE"] == (1.0, 99283.0), "%"] = (
    df.loc[df["PRO_CHARGE"] == (1.0, 99283.0), "TOTAL"].values[0]
    * df.loc[df["PRO_CHARGE"] == (0.0, 99283.0), "PERC_99282"].values[0]
)

Then:

print(df)
# Output

       PRO_CHARGE  TOTAL  PERC_99282     %
0  (0.0, 99283.0)     15       0.115   NaN
1  (1.0, 99283.0)     30       0.490  3.45
2  (1.0, 99284.0)     23       0.530   NaN
Laurent
  • 12,287
  • 7
  • 21
  • 37