79717796

Date: 2025-07-28 19:31:33
Score: 2
Natty:
Report link

I think that pd.cut().value_counts() is what you're looking for.

import pandas as pd
import plotly.express as px

# Example data
data = {
    "data-a": [10, 15, 10, 20, 25, 30, 15, 10, 20, 25],
    "data-b": [12, 18, 14, 22, 28, 35, 17, 13, 21, 27]
}
df = pd.DataFrame(data)


# Define bins
bin_range = range(9, 40, 5)

# Bin data
binned_data_a = pd.cut(df["data-a"], bins=bin_range).value_counts()
binned_data_b = pd.cut(df["data-b"], bins=bin_range).value_counts()
diff = binned_data_a - binned_data_b

# Plot
px.bar(
    x = bin_range[:-1], 
    y = diff.values, 
    labels={"x": "Bin start value", "y": "Difference (a - b)"}
)

histogram showing difference of A and B

Thanks to @Echedey Luis for suggesting .value_counts(). Also see docs for .cut() and .value_counts().

Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @Echedey
  • Low reputation (0.5):
Posted by: drchicken