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!
pd.DataFrame({'a':np.random.randn(6),...is the correct way to write italpha,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.