-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasics.py
More file actions
243 lines (187 loc) · 6.24 KB
/
basics.py
File metadata and controls
243 lines (187 loc) · 6.24 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import numpy as np
import pandas as pd
"""
loc: label-based indexing — uses row/column names.
iloc: integer position-based — uses 0-based position.
at: fast scalar access by label (single cell). iat: fast scalar by position.
"""
def example_loc():
df = pd.DataFrame({
'A': [1, 2, 3],
'B': ['x', 'y', 'z']
})
print(df.loc[0]) # Access the first row by label
# print(df.loc[0, 'A']) # Access a specific value by row label and column name
# print(df.loc[0:1]) # Access a range of rows by label
# print(df.loc[df['A'] > 1]) # Access rows based on a condition
def example_iloc():
df = pd.DataFrame({
'A': [1, 2, 3],
'B': ['x', 'y', 'z']
})
print(df.iloc[0]) # Access the first row by integer position
# print(df.iloc[0, 0]) # Access a specific value by row and column integer position
# print(df.iloc[0:2]) # Access a range of rows by integer position
# print(df.iloc[df['A'] > 1]) # Access rows based on a condition (note: iloc doesn't support boolean indexing directly)
def example_at():
df = pd.DataFrame({
'A': [1, 2, 3],
'B': ['x', 'y', 'z']
})
print(df.at[0, 'A']) # Access a single value by row label and column name
def example_iat():
df = pd.DataFrame({
'A': [1, 2, 3],
'B': ['x', 'y', 'z']
})
# Access a single value by row and column integer position
print(df.iat[0, 0])
"""
GroupBy splits the DataFrame into groups, applies a function, then combines results.
agg reduces each group to one row (e.g. sum per client).
transform returns a Series with the same index as the original — each row gets its group's result.
"""
def example_groupby():
df = pd.DataFrame({
'Category': ['A', 'A', 'B', 'B'],
'Value': [10, 20, 30, 40]
})
grouped = df.groupby('Category')['Value'].sum()
print(grouped)
def example_transform():
df = pd.DataFrame({
'A': [1, 2, 3],
'B': ['x', 'y', 'z']
})
transformed = df['A'].transform(lambda x: x * 2)
print(transformed)
def example_apply():
df = pd.DataFrame({
'A': [1, 2, 3],
'B': ['x', 'y', 'z']
})
applied = df.apply(lambda x: x.sum() if x.dtype ==
'int64' else x.str.upper())
print(applied)
"""
Causes `SettingWithCopyWarning` and how do you fix it?
"""
def setting_with_copy_warning():
df = pd.DataFrame({
'A': [1, 2, 3],
'B': ['x', 'y', 'z']
})
slice_df = df[df['A'] > 1] # This creates a view (not a copy)
# Modifying the view raises a warning
slice_df['B'] = slice_df['B'].str.upper()
print(slice_df)
"""
handle a 10GB CSV without running out of memory?
"""
def handle_large_csv():
import os
chunk_size = 100_000 # Adjust based on available memory
for chunk in pd.read_csv('large_file.csv', chunksize=chunk_size):
# Process each chunk (e.g., filter, aggregate)
processed_chunk = chunk[chunk['value'] > 100] # Example processing
# Append or save results as needed
processed_chunk.to_csv('processed_large_file.csv', mode='a', header=not os.path.exists(
'processed_large_file.csv'), index=False)
"""
Pivot Tables: pivot_table() aggregates data based on specified index/columns.
"""
def example_pivot_table():
df = pd.DataFrame({
'Category': ['A', 'A', 'B', 'B'],
'Subcategory': ['X', 'Y', 'X', 'Y'],
'Value': [10, 20, 30, 40]
})
pivot = df.pivot_table(values='Value', index='Category',
columns='Subcategory', aggfunc='sum')
print(pivot)
"""
How would you compute a portfolio's maximum drawdown using Pandas?
"""
def compute_max_drawdown():
df = pd.DataFrame({
'Date': pd.date_range(start='2024-01-01', periods=5, freq='D'),
'Portfolio Value': [100, 120, 110, 130, 90]
})
df['Cumulative Max'] = df['Portfolio Value'].cummax()
df['Drawdown'] = (df['Portfolio Value'] -
df['Cumulative Max']) / df['Cumulative Max']
max_drawdown = df['Drawdown'].min()
print(f"Maximum Drawdown: {max_drawdown:.2%}")
def run_basics_practice():
data = {
"id": [1, 2, 3, 4, 5, 6],
"name": ["A", "B", "C", "D", "E", "F"],
"age": [23, 45, 31, 22, 35, 28],
"city": ["Delhi", "Mumbai", "Delhi", "Pune", "Mumbai", "Delhi"],
"salary": [50000, 80000, 60000, 45000, 70000, 52000],
"date": pd.date_range(start="2024-01-01", periods=6, freq="D")
}
df = pd.DataFrame(data)
df.to_csv("sample.csv", index=False)
print(df)
print(df.head())
print(df.tail())
print(df.shape)
print(df.columns)
print(df['name'])
print(df[['name', 'age']])
# filtering
df_fil = df[df['age']> 24]
def practice():
# LOAD
orders = pd.read_csv("orders.csv", parse_dates=["order_date"])
customers = pd.read_csv("customers.csv", parse_dates=["signup_date"])
payments = pd.read_parquet("payments.parquet")
# CLEAN
orders = orders[orders["amount"] > 0].drop_duplicates()
customers["segment"] = customers["segment"].fillna("Unknown")
# FILTER
recent_orders = orders.loc[
orders["order_date"] >= "2023-01-01"
]
# MERGE
df = pd.merge(recent_orders, customers, on="customer_id")
df = pd.merge(df, payments, on="order_id")
df = df[df["payment_status"] == "Success"]
# FEATURE ENGINEERING
df["year"] = df["order_date"].dt.year
df["month"] = df["order_date"].dt.month
# GROUPBY
customer_metrics = df.groupby("customer_id").agg({
"order_id": "count",
"amount": ["sum", "mean"],
"order_date": "max"
})
# WINDOW FUNCTION
df["cumulative_spend"] = df.groupby("customer_id")["amount"].cumsum()
# EXPORT
customer_metrics.to_parquet("final_customer_metrics.parquet")
if __name__ == "__main__":
# example_groupby()
# example_transform()
# setting_with_copy_warning()
# example_pivot_table()
run_basics_practice()
"""
Function() OVER (
partition by column
order bu column
)
select emplployee_id, emp_name,
ROW_NUMBER() OVER(
partition by department
order by salary desc
) as row_num
from sales
select emp, dep, salary
dense_rank() over(
partition by dep
order by salary desc
) as dense_rank
from sales ;
"""