You can use pandas package in Python to divide the values in one column of a DataFrame by values in another column, based on a selected condition. Here's an example:
main.py403 chars13 lines
In the above code, we first create a sample DataFrame df
with three columns A, B, and C. Then we use the .loc
method to select only the rows where values in column C is equal to 'yes'. We use the .loc
method again to select only the values in column A and B for those rows. We then divide the values in column A by the values in column B, using the /
operator. Finally, we update the only the selected rows and column A in the original DataFrame df
with the new calculated values.
The resulting output should look like this:
main.py84 chars6 lines
Note that we only updated column A for the selected rows where column C is equal to 'yes'. The other rows remain the same.
gistlibby LogSnag