Pandas How To Uncategorized How to calculate ratio

How to calculate ratio

In this port you see how to calculate ratio in Pandas.

Ratio calculations

You can calculate the ratio between two columns of a pandas DataFrame using the following code:

import pandas as pd

df = pd.DataFrame({'col1': [1, 2, 3, 4, 5], 'col2': [10, 20, 30, 40, 50]})

df['ratio'] = df['col1'] / df['col2']

In this example, the ratio between col1 and col2 is calculated and stored in a new column called ratio. The result will be a DataFrame with three columns: col1, col2, and ratio.

See also:
How to calculate z score
How to calculate percent change
How to calculate mean in Pandas
How to calculate cumulative sum in Pandas

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post