Whenever I stumble across some sort of calculation in Python, I tend to do go for an unpythonic approach because I am not too familiar with the language:
import pandas as pd
import numpy as np
v = 8
gf = 2.5
data_a1 = np.random.randint(5, 10, 21)
data_a2 = np.random.randint(5, 10, 21)
data_a3 = np.random.randint(5, 10, 21)
data_a4 = np.random.randint(5, 10, 21)
data_a5 = np.random.randint(5, 10, 21)
data_b1 = np.random.randint(6, 11, 21)
data_b2 = np.random.randint(6, 11, 21)
data_b3 = np.random.randint(6, 11, 21)
data_b4 = np.random.randint(6, 11, 21)
data_b5 = np.random.randint(6, 11, 21)
e_1 = 2 * (data_a1 + data_b1) / 2 / v / gf
e_2 = 2 * (data_a2 + data_b2) / 2 / v / gf
e_3 = 2 * (data_a3 + data_b3) / 2 / v / gf
e_4 = 2 * (data_a4 + data_b4) / 2 / v / gf
e_5 = 2 * (data_a5 + data_b5) / 2 / v / gf
As you can see from the example above, I explicitly write it down five times instead of using Python how I can imagine it is intended to be used -- I would like to calculate e by updating it on every iteration using a for loop, and I would also prefer to use numpy.
Since all my effort was not bearing fruits, I turned to pandas because I was fairly confident that I could redeem myself for whatever reason:
df_a = pd.DataFrame({'data_a1': data_a1, 'data_a2': data_a2, 'data_a3': data_a3, 'data_a4': data_a4, 'data_a5': data_a5})
df_b = pd.DataFrame({'data_b1': data_b1, 'data_b2': data_b2, 'data_b3': data_b3, 'data_b4': data_b4, 'data_b5': data_b5})
c = 0
dfs = []
for i,j in zip(df_a, df_b):
e = 2 * (i + j) / 2 / v / gf
e = e.add_suffix('_' + str(c))
dfs.addpend(e)
c += 1
Alas, my stupidity prevailed itself and I could not do it either way.
- Is there a streamlined way to work with equations using
numpyso that the variable is updating itself within a for loop that is considered pythonic? - When performing these tasks, is it recommended to stick to
numpyor turn topandas?