'subtract inside DataFrame columns
Would because maybe I did'n sleep this night but, given:
df1 = {'Name':['Tom', 'Jack', 'Steve', 'Ricky'], index=['rank1','rank2','rank3','rank4']}
df2 = {'Age':[28,34,29,42],'bubu':[98,33,3,30], index=['rank1','rank2','rank3','rank4']}
df_merged = = pd.concat([ df1, df2 ], axis = 1)
now, for dynamically manage further arithmetic operation on the columns from a more column dense df:
a = ['Age'] #List
b = ['bubu'] #List
the arithmetic operation:
df_merged['newCol'] = df_merged[a]-df_merged[b]
or both
df_merged.assign(newCol = df_merged[a] - df_merged[b])
I can't subtract the 2 columns because return such error
ValueError: Expected a 1D array, got an array with shape (556, 2)
Solution 1:[1]
df_merged['newCol'] = df_merged['Age']-df_merged['bubu'] or
df_merged.insert(3,'newCol', df_merged['Age']-df_merged['bubu'],True)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | zeeshan12396 |
