I'm looking for the proper way to "iterate" over the rows or - let's say - do the same thing without iteration, as I know that iteration is not the recommended way of handling the data in a dataframe for computations, as explained for instance in this question and in the pandas documentation. To be more precise, let me explain my issue.
I have a dataframe containing start values, end values, and number of steps, e.g.
df_test = pd.DataFrame({"start": [-2.0, -1.0, -5.0 ],
"end": [3.0, 1.0, -1.0],
"n": [6, 3, 9]
})
From this dataframe I would like to create a new column for an existing dataframe which contains concatenated linspaces described by the above start and end points and the number of points. The existing dataframe has the matching shape. My current approach is using list comprehension, then concatenate the arrays to a single array, and then add the column. So:
linspacePts = np.concatenate([np.linspace(s, e, n) for s,e,n in zip(df_test["start"], df_test["end"], df_test["n"])])
df_other["lin. Pts"] = linspacePts
But my first idea was to use df.apply somehow. But I can't figure out how to tell np.linspace
which column corresponds to which argument of the function. At least, I found a workaround, but I was hoping for a better solution, regarding the required detour via a list and numpy array.
Thanks for your help!