- Read the CSV file: Use pd.read_csv to load the CSV file into a pandas DataFrame.
- Define Age Groups by creating a dictionary containing age group conditions using Boolean conditions.
- Segment Visitors by iterating through the dictionary and filter the visitors into respective age groups.
- Visualize the result using matplotlib.
import pandas as pd
import matplotlib.pyplot as plt
visitor_df = pd.read_csv('/content/clustervisitor.csv')
age_groups = {
'Young': (visitor_df['Age'] <= 30),
'Middle-aged': ((visitor_df['Age'] > 30) & (visitor_df['Age'] <= 50)),
'Elderly': (visitor_df['Age'] > 50)
}
visitor_counts = [sum(condition) for condition in age_groups.values()]
age_group_labels = list(age_groups.keys())
plt.figure(figsize=(8, 6))
plt.bar(age_group_labels, visitor_counts, color='skyblue')
plt.xlabel('Age Groups')
plt.ylabel('Number of Visitors')
plt.title('Visitor Distribution Across Age Groups')
plt.show()
for group, condition in age_groups.items():
visitors_in_group = visitor_df[condition]
print(f"Visitors in {group} age group:")
print(visitors_in_group)
print("Visitor segmentation based on age groups complete.")
Visitor segmentation based on age groups successfully completed.