import numpy as np
temp = np.array([22, 24, 25, 23, 26, 28, 27])
print("All temperatures for the week:")
print(temp)
three_days = temp[0:3]
print("\nTemperatures for the first three days:")
print(three_days)
short = temp[:3]
print("\nSame result using shorter slicing syntax:")
print(short)
All temperatures for the week:
[22 24 25 23 26 28 27]
Temperatures for the first three days:
[22 24 25]
Same result using shorter slicing syntax:
[22 24 25]
import numpy as np
readings = np.array([[26.5, np.nan, 24.1, 23.8, np.nan, 25.3, 24.7],
[21.9, 22.2, np.nan, 23.5, 25.8, np.nan, 22.1]])
print("Original sensor readings:")
print(readings)
readings[np.isnan(readings)] = 0
print("\nSensor readings after replacing NaN with 0:")
print(readings)
Original sensor readings:
[[26.5 nan 24.1 23.8 nan 25.3 24.7]
[21.9 22.2 nan 23.5 25.8 nan 22.1]]
Sensor readings after replacing NaN with 0:
[[26.5 0. 24.1 23.8 0. 25.3 24.7]
[21.9 22.2 0. 23.5 25.8 0. 22.1]]
import numpy as np
product_ids = np.array([101, 105, 112, 108])
bin1 = product_ids.copy()
bin2 = product_ids.copy()
print("Bin 1:")
print(bin1)
print("Bin 2:")
print(bin2)
all_products = np.concatenate([bin1, bin2])
print("\nAll product IDs combined:")
print(all_products)
Bin 1:
[101 105 112 108]
Bin 2:
[101 105 112 108]
All product IDs combined:
[101 105 112 108 101 105 112 108]
import numpy as np
q1_sales = np.array([500, 520, 550])
q2_sales = np.array([580, 600, 620])
print("Q1 Sales:")
print(q1_sales)
print("Q2 Sales:")
print(q2_sales)
quarterly_report = np.vstack([q1_sales, q2_sales])
print("\nQuarterly Sales Report:")
print(quarterly_report)
Q1 Sales:
[500 520 550]
Q2 Sales:
[580 600 620]
Quarterly Sales Report:
[[500 520 550]
[580 600 620]]
import numpy as np
scores = np.array([85, 92, 78, 88, 95, 76])
print("All scores:")
print(scores)
highscores = scores[scores >= 90]
print("\nScores greater than or equal to 90:")
print(highscores)
All scores:
[85 92 78 88 95 76]
Scores greater than or equal to 90:
[92 95]
#pandas
Double-click (or enter) to edit
Start coding or generate with AI.
keyboard_arrow_down pandas
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Department': ['HR', 'Engineering', 'Sales'],
'Salary': [60000, 85000, 75000]}
df = pd.DataFrame(data)
print("Employee DataFrame:")
print(df)
print("\nBob's data using .loc:")
bob_data_loc = df.loc[df['Name'] == 'Bob']
print(bob_data_loc)
print("\nBob's data using .iloc:")
bob_data_iloc = df.iloc[1]
print(bob_data_iloc)
Employee DataFrame:
Name Department Salary
0 Alice HR 60000
1 Bob Engineering 85000
2 Charlie Sales 75000
Bob's data using .loc:
Name Department Salary
1 Bob Engineering 85000
Bob's data using .iloc:
Name Bob
Department Engineering
Salary 85000
Name: 1, dtype: object
import pandas as pd
freshmen = pd.DataFrame({
'StudentID': [1001, 1002, 1003],
'Major': ['Computer Science', 'Biology', 'Mathematics']
})
sophomores = pd.DataFrame({
'StudentID': [2001, 2002, 2003],
'Major': ['Engineering', 'Psychology', 'Physics']
})
print("Freshmen DataFrame:")
print(freshmen)
print("\nSophomores DataFrame:")
print(sophomores)
all_students = pd.concat([freshmen, sophomores], ignore_index=True)
print("\nAll Students DataFrame:")
print(all_students)
Freshmen DataFrame:
StudentID Major
0 1001 Computer Science
1 1002 Biology
2 1003 Mathematics
Sophomores DataFrame:
StudentID Major
0 2001 Engineering
1 2002 Psychology
2 2003 Physics
All Students DataFrame:
StudentID Major
0 1001 Computer Science
1 1002 Biology
2 1003 Mathematics
3 2001 Engineering
4 2002 Psychology
5 2003 Physics
import pandas as pd
projects = pd.DataFrame({
'project_id': [101, 102, 103],
'project_name': ['Website Redesign', 'Mobile App', 'Data Analysis']
})
employees = pd.DataFrame({
'employee_id': [201, 202, 203, 204],
'project_id': [101, 102, 101, 103]
})
print("Projects DataFrame:")
print(projects)
print("\nEmployees DataFrame:")
print(employees)
merged_data = pd.merge(employees, projects, on='project_id')
print("\nMerged DataFrame:")
print(merged_data)
Projects DataFrame:
project_id project_name
0 101 Website Redesign
1 102 Mobile App
2 103 Data Analysis
Employees DataFrame:
employee_id project_id
0 201 101
1 202 102
2 203 101
3 204 103
Merged DataFrame:
employee_id project_id project_name
0 201 101 Website Redesign
1 202 102 Mobile App
2 203 101 Website Redesign
3 204 103 Data Analysis
import pandas as pd
import numpy as np
customers = pd.DataFrame({
'CustomerID': [1, 2, 3, 2, 4, 5],
'Name': ['Alice', 'Bob', 'Charlie', 'Bob', 'Diana', 'Eve'],
'Phone Number': ['123-456-7890', np.nan, '555-123-4567', np.nan, np.nan, '999-888-7777']
})
print("Original Customer DataFrame:")
print(customers)
customers_cleaned = customers.drop_duplicates().copy()
print("\nAfter removing duplicates:")
print(customers_cleaned)
Original Customer DataFrame:
CustomerID Name Phone Number
0 1 Alice 123-456-7890
1 2 Bob NaN
2 3 Charlie 555-123-4567
3 2 Bob NaN
4 4 Diana NaN
5 5 Eve 999-888-7777
After removing duplicates:
CustomerID Name Phone Number
0 1 Alice 123-456-7890
1 2 Bob NaN
2 3 Charlie 555-123-4567
4 4 Diana NaN
5 5 Eve 999-888-7777
import pandas as pd
sales_data = pd.DataFrame({
'Region': ['East', 'West', 'North', 'East', 'South', 'East', 'West'],
'Revenue': [15000, 12000, 18000, 22000, 9000, 17000, 14000]
})
print("Sales Data DataFrame:")
print(sales_data)
east_data = sales_data[sales_data['Region'] == 'East']
print("\nEast Region Data:")
print(east_data)
total_east_revenue = east_data['Revenue'].sum()
print(f"\nTotal Revenue for East Region: {total_east_revenue}")
Sales Data DataFrame:
Region Revenue
0 East 15000
1 West 12000
2 North 18000
3 East 22000
4 South 9000
5 East 17000
6 West 14000
East Region Data:
Region Revenue
0 East 15000
3 East 22000
5 East 17000
Total Revenue for East Region: 54000
Start coding or generate with AI.