Introduction to NumPy | Screen 12

# we'll compare against the first 5 rows only
taxi_first_five = taxi[:5]
# select these columns: fare_amount, fees_amount, tolls_amount, tip_amount
fare_components = taxi_first_five[:,9:13]

fare_sums = fare_components.sum(axis=1)
fare_totals = taxi_first_five[:,13]

How come the output is the same
taxi_firs-five is selecting the first 5 rows and 13th column, which is total
fare_components is selecting all 4 rows and adding up

but the output is the same; I did not understand that part
How come output is both 5 rows, because fare_components has all rows


[ 69.99 54.3 37.8 32.76 18.8]
[ 69.99 54.3 37.8 32.76 18.8]

taxi_first_five stores the first 5 rows and all the columns from taxi

fare_components stores all rows and columns 9 to 12 (including 12th). These rows and columns are from taxi_first_five.

Note: When we use slicing for a numpy array, like we did for lists, 9:13 means 9, 10, 11, 12. The 13 is not inclusive.

Then we sum the columns in fare_components for each row

So, the above will return 5 values (because we have 5 rows), where each value is the sum of values from all the columns - 9, 10, 11, 12. We store these in fare_sums.

fare_totals stores the values for all rows and the 13th column from taxi_first_five.