perform math operation between scalar and pandas dataframe

Question:

In an existing code, I used the following code to perform operation against a dataframe column.

df.loc[:, ['test1']] = m/(df.loc[:, ['rh']]*d1)

Here, both m and d1 are scalar. ‘test1’ and ‘rh’ are the column names.

Is this the right way or the best practice to perform math operation against the dataframe?

Asked By: user288609

||

Answers:

Yes, what you have there is fine. If you’re looking for ways to improve it, a couple suggestions:

  • When accessing entire columns (like you’re doing here), you can be more concise by not using .loc, and just doing df["test1"] and df["rh"]
  • You could alternatively use the .apply() method, which is useful for a more general case of wanting to perform any arbitrary operation (that you can implement in a function, anyway) on a DataFrame column. You could use it here, and it would look like
    df["test1"] = df["rh"].apply(lambda rh: m/(rh*d1)), though it is almost certainly not necessary for this simple case.
Answered By: L0tad