-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathPandaz.py
More file actions
29 lines (23 loc) · 871 Bytes
/
Pandaz.py
File metadata and controls
29 lines (23 loc) · 871 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import pandas as pd # We give the elder a short, respectful nickname 'pd'
# The scribe creates a table from a dictionary
data = {'Name': ['Lebo', 'Thando', 'Sipho'], 'Age': [25, 30, 35]}
df = pd.DataFrame(data)
# Display the beautiful table
print(df)
# Calculate the average age of the village
print(f"\nThe average age is: {df['Age'].mean()} Summers")
#Add a new column for Occupation
df['Occupation'] = ['Farmer', 'Blacksmith', 'Doctor']
print("\nUpdated Table with Occupation:")
print(df)
# Filter villagers older than 28
Adults = df[df['Age'] > 28]
print("\nVillagers Adults:")
print(Adults)
# Sort villagers by Age
Sorted_by_Age = df.sort_values(by='Age')
print("\nVillagers Sorted by Age:")
print(Sorted_by_Age)
# Save the table to a CSV file
df.to_csv('villagers.csv', index=False)
print("\nThe villagers' data has been saved to 'villagers.csv'.")