0

My current dataset is in the following format.

df = pd.DataFrame({
'a' = np.random.randn(6),
'b' = np.random.randn(6),
'c' = np.random.randn(6),
'd' = np.random.randn(6),
'e' = np.random.randn(6),
'f' = np.random.randn(6)
})

My updated dataset looks like this:

df = pd.DataFrame({
'a' = np.random.randn(6),
'b' = np.random.randn(6),
'c' = np.random.randn(6),
'd' = np.random.randn(6),
'e' = np.random.randn(6),
'f' = np.random.randn(6),
'alpha' = np.random.randn(6),
'beta' = np.random.randn(6),
'gamma' = np.random.randn(6)
    })


where alpha = df['a']*2
beta = df['b']*5
gamma = df['c']*6 

Both the final 3 columns (alpha, beta & gamma) & input columns (a,b,c) and numeric values (2,5,6) are variables so getting pass from outside as list.

So, I was trying this following code:

newCol = ['alpha', 'beta', 'gamma']
oldCol = ['a', 'b', 'c']
numVal = [2, 5, 6]

for i in range(len(newCol)):
     
      df[newCol[i]] = df[oldCol[i]]*numVal[i]

But I'm getting the following error:

TypeError: unsupported operand type(s) for /: 'str' and 'float'

Can someone please tell me how to solve this problem?

Thanks!

4
  • pd.DataFrame({'a':np.random.randn(6),... is the correct way to write it Commented Oct 8, 2020 at 12:51
  • Which line gives you the error? Commented Oct 8, 2020 at 12:51
  • @gtomer: The updated dataset will have alpha, beta & gamma. I created the dummy dataset, so that the format of the data can be clear. The values of these columns given by the calculation (multiplication of original column with some number). The loop is throwing the error. There must be some dynamic way to create new column. Commented Oct 8, 2020 at 12:57
  • @Beta Actually your solution works without an error. I just tried it now. Commented Oct 11, 2020 at 10:43

2 Answers 2

2

IIUC, then you can use this:

df[newCol] = df[oldCol].mul(numVal, axis=1)
df

Output:

          a         b         c         d         e         f     alpha       beta     gamma
0 -0.725042 -0.756909  0.070109  0.850219  1.792450  1.324259 -1.450084  -3.784547  0.420656
1 -0.062379 -0.105332 -1.222571 -1.297675 -0.514850  0.610115 -0.124758  -0.526659 -7.335425
2  1.396269 -0.602535  0.033669  1.190688  0.335367  1.287264  2.792537  -3.012675  0.202014
3  0.303298 -2.016903  0.748448  0.317952 -0.319775 -1.023614  0.606595 -10.084517  4.490689
4 -0.469161 -2.133385 -0.488404  1.322836  0.560578  1.436018 -0.938321 -10.666927 -2.930423
5 -0.177657 -1.065320 -0.103551  0.406531  0.128109  0.387128 -0.355314  -5.326601 -0.621305
Sign up to request clarification or add additional context in comments.

2 Comments

This answer is sleek. But Mayank's is more readable. But thanks for your asnwer.
@Beta Happy coding and be safe and stay healthy.
1

You can do this using zip function and a simple for loop:

In [24]: for i,j,k in list(zip(numVal, oldCol, newCol)):
    ...:     df[k] = df[j] * i
    ...: 

In [25]: df
Out[25]: 
          a         b         c         d         e         f     alpha      beta      gamma
0  0.737824  1.085417  1.369061  0.565950 -2.075468 -0.929977  1.475647  5.427085   8.214369
1 -1.337789  0.278580 -1.880562  0.082981 -0.487926  0.553878 -2.675578  1.392898 -11.283371
2 -0.711687 -0.698380  2.051574 -0.846402 -0.580580  0.031335 -1.423375 -3.491900  12.309442
3  0.239819 -1.092086  0.734303 -3.621612 -0.111699  1.675823  0.479638 -5.460428   4.405819
4 -0.320136 -0.406935  0.680605 -0.711390  0.176590  0.177886 -0.640271 -2.034674   4.083632
5 -0.174353 -0.750984 -0.057860  1.696935 -0.210906  2.786231 -0.348707 -3.754922  -0.347160

1 Comment

Great. Glad to help.

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.