-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPython_task_1.py
175 lines (102 loc) · 3.86 KB
/
Python_task_1.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python
# coding: utf-8
# # Question 1
# In[6]:
import pandas as pd
def generate_car_matrix(df):
# Selecting relevant columns
car_data = df[['id_1', 'id_2', 'car']].copy()
# Creating a pivot table with id_1 as index, id_2 as columns, and car as values
car_matrix = car_data.pivot(index='id_1', columns='id_2', values='car')
# Filling NaN values with 0 and setting diagonal values to 0
car_matrix = car_matrix.fillna(0).astype(float)
return car_matrix
df = pd.read_csv('dataset-1.csv')
result = generate_car_matrix(df)
print(result)
# # Question 2
# In[10]:
import pandas as pd
def get_type_count(df):
# Add a new categorical column car_type based on values of the column car
conditions = [
(df['car'] <= 15),
(df['car'] > 15) & (df['car'] <= 25),
(df['car'] > 25)
]
choices = ['low', 'medium', 'high']
df['car_type'] = pd.cut(df['car'], bins=[-float('inf'), 15, 25, float('inf')], labels=choices, right=False)
# Calculate the count of occurrences for each car_type category
type_counts = df['car_type'].value_counts().to_dict()
# Sort the dictionary alphabetically based on keys
sorted_type_counts = dict(sorted(type_counts.items()))
return sorted_type_counts
df = pd.read_csv('dataset-1.csv')
result = get_type_count(df)
print(result)
# # Question 3
#
# In[11]:
import pandas as pd
def get_bus_indexes(df):
# Calculate the mean value of the bus column
mean_bus = df['bus'].mean()
# Identify indices where bus values are greater than twice the mean
bus_indexes = df[df['bus'] > 2 * mean_bus].index.tolist()
# Sort the indices in ascending order
bus_indexes.sort()
return bus_indexes
df = pd.read_csv("dataset-1.csv")
bus_indices = get_bus_indexes(df)
print(bus_indices)
# # Question 4
# In[19]:
import pandas as pd
def filter_routes(df):
# Calculate the average of the truck column for each route
avg_truck_by_route = df.groupby('route')['truck'].mean()
# Filter routes where the average truck value is greater than 7
selected_routes = avg_truck_by_route[avg_truck_by_route > 7].index
# Sort and return the selected routes as a list
return sorted(selected_routes)
df = pd.read_csv('dataset-1.csv')
selected_routes = filter_routes(df)
print(selected_routes)
# # Question 5
# In[26]:
import pandas as pd
def multiply_matrix(car_matrix):
# Apply the specified logic to modify values
modified_matrix = car_matrix.applymap(lambda x: x * 0.75 if x > 20 else x * 1.25)
# Round values to 1 decimal place
modified_matrix = modified_matrix.round(1)
return modified_matrix
# Example usage:
df = pd.read_csv('dataset-1.csv')
car_matrix = generate_car_matrix(df)
result_matrix = multiply_matrix(car_matrix)
print(result_matrix)
# In[27]:
result_matrix
# # Question 6
# In[34]:
import pandas as pd
def verify_timestamps(df):
# Combine 'startDay' and 'startTime' to create 'startTimestamp'
df['startTimestamp'] = pd.to_datetime(df['startDay'] + ' ' + df['startTime'], format='%A %H:%M:%S')
# Combine 'endDay' and 'endTime' to create 'endTimestamp'
df['endTimestamp'] = pd.to_datetime(df['endDay'] + ' ' + df['endTime'], format='%A %H:%M:%S')
# Check if the timestamps are within a full 24-hour period
full_day_check = (df['endTimestamp'] - df['startTimestamp']) == pd.Timedelta(days=1)
# Check if the timestamps span all 7 days of the week
all_days_check = df.groupby(['id', 'id_2'])['startDay'].nunique() == 7
# Combine both checks using the same index as the original DataFrame
result = pd.DataFrame({'full_day_check': full_day_check, 'all_days_check': all_days_check}).all(axis=1)
return result
# Load your dataset
df = pd.read_csv('dataset-2.csv')
# Apply the function
result_series = verify_timestamps(df)
# Print the result
print(result_series)
# In[ ]: