calculate percentile of a dataframe column in python

To calculate the percentile of a column in a DataFrame using Python, you can use the numpy.percentile() function.

Here's an example of how you can do it:

main.py
import pandas as pd
import numpy as np

# Create DataFrame
data = {
    'column1': [10, 20, 30, 40, 50],
    'column2': [40, 50, 60, 70, 80]
}
df = pd.DataFrame(data)

# Calculate percentile of a column
percentile = np.percentile(df['column1'], 75)

print(f"The 75th percentile of 'column1' is: {percentile}")
310 chars
15 lines

In this example, we create a DataFrame with two columns (column1 and column2). We calculate the 75th percentile of column1 using np.percentile() function. The result is stored in the percentile variable and printed to the console.

You can replace 75 with any desired percentile value to calculate the respective percentile for your DataFrame column.

related categories

gistlibby LogSnag