Search code examples
pythonpowerbifilteringhistogram

Python: How to add a filter to a histogram


I use Python in Power BI and have currently the following code:

import matplotlib.pyplot as plt
plt.hist(dataset.age, bins=10, edgecolor="#6A9662", color="#DDFFDD", alpha=0.75)
plt.show()

In my table i have a column TYPE with "E" and "G". How can i add a Filter TYPE = "E" to the Python code?

Many thanks in advance!

I am expecting that my histogram shows only results for the filtered table TYPE = "E".


Solution

  • I suggest you to filter the dataframe before plotting

    import matplotlib.pyplot as plt
    
    data = dataset[dataset["TYPE"]=="E"].age
    plt.hist(data, bins=10, edgecolor="#6A9662", color="#DDFFDD", alpha=0.75)
    plt.show()