divide table row by another from label in python

One way to divide a row in a Pandas DataFrame by another is by using label-based indexing. Here is an example code block:

main.py
import pandas as pd

# create a sample DataFrame
df = pd.DataFrame({'A': [10, 20, 30], 'B': [5, 10, 15]}, 
                  index=['row1', 'row2', 'row3'])

# divide row2 by row1
result = df.loc['row2'] / df.loc['row1']

print(result)
236 chars
11 lines

In this example, we first create a sample DataFrame df with two columns A and B. We then use label-based indexing to select row2 and row1 using df.loc['row2'] and df.loc['row1'] respectively. We then divide row2 by row1 using the "/" operator.

The output of this code block will be a new Series which shows the result of dividing row2 by row1 as follows:

main.py
A    2.000000
B    2.000000
dtype: float64
43 chars
4 lines

gistlibby LogSnag