Search code examples
pythondataframepercentage

Calculate percentage for range of values in a dataframe column python


I got a dataset named df_blue where i got two columns ("colour", and "length") and 7291 rows with different values. see image as a sample of this dataset (https://ibb.co/vYjDD8Q)

I need to know what is the percentage of total length in range : >15.625 and <17.755 (tolerences are inclusive)

Thank you in advance


Solution

  • The following will give what you want:

    df_blue[(df_blue['Length'] > 15.625) & (df_blue['Length'] < 17.755)]['Length'].sum() / df_blue['Length'].sum()