Miscellanea there are a few more table methods youll

This preview shows page 9 - 11 out of 12 pages.

6. Miscellanea There are a few more table methods you'll need to fill out your toolbox. The first 3 have to do with manipulating the columns in a table. The table farmers_markets.csv contains data on farmers' markets in the United States (data collected by the USDA). Each row represents one such market. Question 6.1. Load the dataset into a table. Call it farmers_markets . In [ ]: farmers_markets = ... farmers_markets In [ ]: _ = ok.grade( 'q6_1' ) You'll notice that it has a large number of columns in it! num_columns Question 6.2. The table property num_columns (example call: tbl.num_columns ) produces the number of columns in a table. Use it to find the number of columns in our farmers' markets dataset. In [ ]: num_farmers_markets_columns = ... print( "The table has" , num_farmers_markets_columns, "columns in it!" In [ ]: _ = ok.grade( 'q6_2' ) Most of the columns are about particular products -- whether the market sells tofu, pet food, etc. If we're not interested in that stuff, it just makes the table difficult to read. This comes up more than you might think. )
select In such situations, we can use the table method select to pare down the columns of a table. It takes any number of arguments. Each should be the name or index of a column in the table. It returns a new table with only those columns in it. For example, the value of imdb.select("Year", "Decade") is a table with only the years and decades of each movie in imdb . Question 6.3. Use select to create a table with only the name, city, state, latitude ('y'), and longitude ('x') of each market. Call that new table farmers_markets_locations In [ ]: farmers_markets_locations = ... farmers_markets_locations In [ ]: _ = ok.grade( 'q6_3' ) select is not column The method select is definitely not the same as the method column farmers_markets.column('y') is an array of the latitudes of all the markets. farmers_markets.select('y') is a table that happens to contain only 1 column, the latitudes of all the markets. Question 6.4. Below, we tried using the function np.average to find the average latitude ('y') and average longitude ('x') of the farmers' markets in the table, but we screwed something up. Run the cell to see the (somewhat inscrutable) error message that results from calling np.average on a table. Then, fix our code. In [ ]: average_latitude = np.average(farmers_markets.select( 'y' )) average_longitude = np.average(farmers_markets.select( 'x' )) print ( "The average of US farmers' markets' coordinates is located at (" , average_latitude, "," , average_longitude, ")" In [ ]: _ = ok.grade( 'q6_4' ) drop drop serves the same purpose as select , but it takes away the columns you list instead of the ones you don't list, leaving all the rest of the columns. Question 6.5. Suppose you just didn't want the "FMID" or "updateTime" columns in farmers_markets . Create . ! . )

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture