Home > database >  Multiply dataframe column by list of scalars and create a new column for each multiplication
Multiply dataframe column by list of scalars and create a new column for each multiplication

Time:01-06

I have a dataframe

                  Date     Date.1 SYMBL   TF    Pattern  ...    Stop Risk Amount  GAP LVL  Actual / Back Test  Notes
0             00:00:00 2022-12-12  QCOM   5M  Buy Setup  ...  117.46        0.45      NaN              Actual    NaN
1  1900-01-01 00:00:00 2022-12-12  QCOM   5M       0.84  ...  117.46        0.45      NaN           Back Test    NaN
2  1900-01-02 00:00:00 2022-11-29  FUTU  15M  Buy Setup  ...   58.15        0.63      NaN              Actual    NaN

I want to take the column "Risk Amount" and multiply it by a list l = [2, 3, 4, 5] and store each multiplication in a new column c2, c3, c4, c5 corresponding to each digit in the list.

For example, column 2 c2 should look like:

c2
0.9   # 0.45 * 2
0.9   # 0.45 * 2
1.26  # 0.63 * 2

and column 3 c3 should look like:

c3
1.35   # 0.45 * 3
1.35   # 0.45 * 3
1.89   # 0.63 * 3

What I tried to do:

# This allows us to iterate over a list of numbers [0.1, 0.2, 0.3, ..., 9.9, 10]
steps = [ i for i in np.linspace(0.1, 10, 100) ]
for i in np.linspace(0.1, 10, 100):
    column_title = f"c{i:.1f}"
    df[column_title] = df_long["Risk Amount"] * i

but this gives the following warning


PerformanceWarning: DataFrame is highly fragmented.  This is usually the result of calling `frame.insert` many times, which has poor performance.  Consider joining all columns at once using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.copy()`

CodePudding user response:

Lets do outer product

c = list(map('c{}'.format, l))
df[c] = np.multiply.outer(list(df['Risk Amount']), l)

     c2    c3    c4    c5
0  0.90  1.35  1.80  2.25
1  0.90  1.35  1.80  2.25
2  1.26  1.89  2.52  3.15

CodePudding user response:

you can create a new dataframe using list comprehension:

df = pd.DataFrame(data={f'c{i:.1f}': [i * _ for _ in df_long['Risk Amount'].values] for i in np.linspace(0.1, 10, 100)})
  • Related