I’m reading Python for Data Analysis by Wes Mckinney, but I was surprised by this data manipulation. You can see all the procedure here but I will try to summarize it here. Assume you have something like this:
In [133]: agg_counts = by_tz_os.size().unstack().fillna(0)
Out[133]:
a Not Windows Windows
tz 245 276
Africa/Cairo 0 3
Africa/Casablanca 0 1
Africa/Ceuta 0 2
Africa/Johannesburg 0 1
Africa/Lusaka 0 1
America/Anchorage 4 1
...
tz means time zone and Not Windows and Windows are categories extracted from the User Agent in the original data, so we can see that there are 3 Windows users and 0 Non-windows users in Africa/Cairo from the data collected.
Then in order to get “the top overall time zones” we have:
In [134]: indexer = agg_counts.sum(1).argsort()
Out[134]:
tz
24
Africa/Cairo 20
Africa/Casablanca 21
Africa/Ceuta 92
Africa/Johannesburg 87
Africa/Lusaka 53
America/Anchorage 54
America/Argentina/Buenos_Aires 57
America/Argentina/Cordoba 26
America/Argentina/Mendoza 55
America/Bogota 62
...
So at that point, I would have thought that according to the documentation I was summing over columns (in sum(1)) and then sorting according to the result showing arguments (as usual in argsort). First of all, I’m not sure what does it mean “columns” in the context of this series because sum(1) is actually summing Not Windows and Windows users keeping that value in the same row as its time zone. Furthermore, I can’t see a correlation between argsort values and agg_counts. For example, Pacific/Auckland has an “argsort value” (in In[134]) of 0 and it only has a sum of 11 Windows and Not Windows users. Asia/Harbin has an argsort value of 1 and appears with a sum of 3 Windows and Not Windows users.
Can someone explain to me what is going on there? Obviously I’m misunderstanding something.
sum(1)means sum overaxis = 1. The terminology comes fromnumpy.For a 2+ dimensional object, the 0-axis refers to the rows. Summing over the 0-axis means summing over the rows, which amounts to summing “vertically” (when looking at the table).
The 1-axis refers to the columns. Summing over the 1-axis means summing over the columns, which amounts to summing “horizontally”.
numpy.argsortreturns an array of indices which tell you how to sort an array. For example:The 2 in the array returned by
np.argsortmeans the smallest value inxisx[2], which equals1. The next smallest isx[4]which is also 1. And so on.If we define
then
totals.argsort()is argsorting the values[521, 3, 1, 2, 1, 1, 5]. We’ve seen the result; it is the same asnumpy.argsort:These values are simply made into a
Series, with the sameindexastotals:Associating the
totals.indexwith this argsort indices does not appear have intrinsic meaning, but if you computetotals[totals.argsort()]you see the rows oftotalsin sorted order: