Let’s take the example of the titanic dataset. The dataset contains various information such as age, embark town of the passengers, whether the passenger survived, etc. Let’s say we want to know how many passengers onboarded from each embark town. To know that we can plot a bar chart or bar plot.
We can use the DataFrame.plot.bar() function to plot a bar chart. We need to first know the number of passengers from each embark town. To know that, we can use the value_counts() function (How to find the count of unique records for all the unique values in a column of a DataFrame?).
import pandas from matplotlib import pyplot df = pandas.read_csv("titanic.csv") print(df.info()) df.embark_town.value_counts().plot.bar() pyplot.show()
The df.embark_town.value_counts().plot.bar() function here plots a bar plot that shows the number of passengers from each embark town. The output bar plot will look like the following:






0 Comments