I have a dataframe only containing peoples ages and a want to make a bar chart with matplotlib. In order to do this i want to organize the data from the age column into groups like 0-4, 5-9 etc. How do I go about making a bar chart by putting the ages into groups?
            Asked
            
        
        
            Active
            
        
            Viewed 294 times
        
    0
            
            
        - 
                    Could you share some of the data in your dataframe? See: https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – BrokenBenchmark May 04 '22 at 03:55
1 Answers
0
            
            
        You can firstly assign each age group a numerical value, make it a new column in your dataframe, and then use this to plot a seaborn countplot. I am using countplot instead of barplot because I assume that you want to plot the counts of each group on the graph, and for this countplot is the most efficient method. If not then let me know.
- Creating random data (Run the code below to work on the exact same data as me) -
np.random.seed(42)
ages = [np.random.randint(0, 80) for _ in range(10000)]
df = pd.DataFrame(ages, columns = ["Age"])
- Making age groups -
age_groups = [range(age, age + 5) for age in range(0, 80, 5)]
- Assigning each age a group -
age_group = []
for age in ages:
  for i, group in enumerate(age_groups):
    if age in group:
      age_group.append(i)
      break
- Making it a new column in the dataframe -
df["Age Group"] = age_group
- Plotting a seaborn countplot-
import seaborn as sns
plt.figure(figsize = (20,5))
sns.countplot(x = "Age Group", data = df)
plt.show()
Output -
 
    
    
        Zero
        
- 1,800
- 1
- 5
- 16
 
    