I have a dataframe that for each row, I want to create list of 100 numbers (step 1), then multiply these lists together (step 2) and finally have a dataframe with the result (step 3). I can do this for one row but I'm struggling with how to write a for loop to do this for all of the rows. Using a nested loop or another method would also be fine.
Starting with the dataframe, orig:
import numpy as np
import pandas as pd
orig = pd.DataFrame(np.array([['a', 2.09328, 11.4043282, 0.1, 1.1], ['b', 5.985439, 6.59949, 0.3, 0.19], ['c', 8.5543045, 9.5402459, 0.09, 1.2]]),
columns=['site', 'x_min', 'x_max','y_min','y_max'])
orig = orig.set_index('site')
I want to create two new variables, x and y for each row in orig:
# Step 1: Create two new variables x and y for each row. For example, for the first row, site a, this would look like this:
x_a = np.linspace(2.09328,11.4043282,100)
y_a = np.linspace(0.1, 1, 100)
Then for each row, I want to multiply the x and y variables along with a constant z:
# Step 2: For each site, multiply the x and y arrays together with another variable z
z = 24
pd.DataFrame(x_a*y_a*24)
And then for Step 3 I want to have a dataframe where each column name is the row in orig (so, "a", "b", "c") and the rows are the product from the previous calculation, so xyz. The shape for this final dataframe should be three columns by 100 rows.
All I have so far is this and it's not working too well for me:
# So far all I have for step 1 is this:
xs = []
ys = []
#for each row in dataframe
for i in range(orig.shape[0]):
row = orig.iloc[i:,]
_xs = np.linspace(row['x_min'], row['x_max'], 100)
_ys = np.linspace(row['y_max'], row['y_min'],100)
print(_xs)