You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
1 '7 120% AMI +' '5 80 - 100% AMI' '7 120% AMI +' '.'
66
+
2 '7 120% AMI +' '6 100 - 120% AMI' '7 120% AMI +' '.'
67
+
68
+
FMTBURDEN FMTREGION FMTSTATUS
69
+
0 '3 Greater than 50%' '-5' '-5'
70
+
1 '1 Less than 30%' '-5' '-5'
71
+
2 '1 Less than 30%' '-5' '-5'
72
+
73
+
[3 rows x 99 columns]
74
+
75
+
22
76
###2: Lists
23
77
24
78
Now that we have read in both datasets into DataFrame objects, let's add them to a List. A List is type of object (just like a DataFrame, Integer, or String) that contains an ordered group of objects. Just like how a grocery list contains a group of "ingredient" objects to buy, is a List object in Python houses a group of the objects we add to it. Instead of writing code to manipulate each object separately (in our case the objects are DataFrames), we can group a few objects into a List object, write the logic once, and apply it to every object in that List. This saves us a lot of time and energy, and will be important when we deal with much larger datasets with tens, hundreds or even thousands of DataFrames.
@@ -27,13 +81,13 @@ In the following code block, we will create an empty List called data_frames_lis
27
81
28
82
Then, we will add a year column for each DataFrame to keep track of which DataFrame is which:
29
83
30
-
housing_2005['year'] = '2005'
31
-
housing_2007['year'] = '2007'
84
+
housing_2005['year'] = '2005'
85
+
housing_2007['year'] = '2007'
32
86
33
87
Each row now has a value for year, either 2005 or 2007, identifying which dataset that row originated from. Finally, we will use .append() to first add housing_2005 then housing_2007 to the end of data_frames_list. The List object preserves the order by which the DataFrames were added:
34
88
35
-
data_frames_list.append(housing_2005)
36
-
data_frames_list.append(housing_2007)
89
+
data_frames_list.append(housing_2005)
90
+
data_frames_list.append(housing_2007)
37
91
38
92
The list now contains these two DataFrames in the order we added them.
we assigned the column names to a list object, called columns:
133
188
134
-
columns = ['AGE1', 'FMR', 'TOTSAL', 'year']
189
+
columns = ['AGE1', 'FMR', 'TOTSAL', 'year']
135
190
136
191
and passed it into the filter criteria:
137
192
138
-
filtered_df = df[columns]
193
+
filtered_df = df[columns]
139
194
140
195
Instead of creating two different DataFrame objects (like filtered_housing_2005, filtered_housing_2007, etc), we created an empty list called new_df_list:
141
196
142
-
new_df_list = list()
197
+
new_df_list = list()
143
198
144
199
and appended each of the filtered_df objects to it:
145
200
146
-
new_df_list.append(filtered_df)
201
+
new_df_list.append(filtered_df)
147
202
148
203
As you can see, we placed a heavy emphasis on abstracting, or generalizing, our logic so we can detail the logic once, and apply it in many cases. The filter_columns function that we wrote is essentially a piece of software that will filter any list of DataFrame objects into the 4 columns we want. Whether the list of DataFrame objects has 1 DataFrame object or 25, the same function can be applied to get the result we want. Another abstraction we could implement would be to modify the function and specify the columns we want filtered every time by adding it as an input to the function (alongside data_frames_list). This way, instead of always using a specific set of columns within the function, the user can now specify in the input which columns they prefer to filter their DataFrames.
149
204
@@ -158,11 +213,11 @@ Let's quickly verify that each of the DataFrame objects in filtered_data_frames_
158
213
# For every dataframe in the list 'filtered_data_frames_list'.
we use the function str() to convert Integer objects, like year and len(negative_age_count), into String objects. The print function can only print String objects, so we must convert other objects to String objects. While not all objects can be converted to String objects for displaying, most can and we will cover in a later lesson how we can tell.
181
236
@@ -188,7 +243,7 @@ for df in filtered_data_frames_list:
@@ -209,15 +264,15 @@ Now let's write a function clean_rows() that takes a List of DataFrames and retu
209
264
210
265
Inside the function, we will first instantiate, or create, an empty list with no elements:
211
266
212
-
cleaned_list = list()
267
+
cleaned_list = list()
213
268
214
269
Then, we will iterate through each DataFrame in filtered_data_frames_list, create a temporary DataFrame cleaned_df containing just the positive AGE1 rows for each DataFrame:
215
270
216
-
cleaned_df = df[ df ['AGE1'] > 0 ]
271
+
cleaned_df = df[ df ['AGE1'] > 0 ]
217
272
218
273
And then we will append cleaned_df to cleaned_list for each iteration:
219
274
220
-
cleaned_list.append(cleaned_df)
275
+
cleaned_list.append(cleaned_df)
221
276
222
277
Let's run this function clean_rows on data_frames_list and assign the results to cleaned_data_frames_list.
0 commit comments