216k views
5 votes
In order to investigate further, it will be helpful to determine what region of the United States each city was located in: Northeast, Northwest, Southeast, or Southwest. For our purposes, we will be using the following geographical boundaries: 1. A station is located in the "Northeast" region if its latitude is above or equal to 40 degrees and its longtitude is greater than or equal to -100 degrees. 2. A station is located in the "Northwest" region if its latitude is above or equal to 40 degrees and its longtitude is less than -100 degrees. 3. A station is located in the "Southeast" region if its latitude is below 40 degrees and its longtitude is greater than or equal to -100 degrees. 4. A station is located in the "Southwest" region if its latitude is below 40 degrees and its longtitude is less than -100 degrees.

Question 1.1.5: Add a new column in cities labeled Region that contains the region in which the city is located. For full credit, you must use the coordinates_to_region function you defined rather than reimplementing its logic.

2 Answers

4 votes

Final answer:

To determine the region of cities in the United States, use the given criteria and the 'coordinates_to_region' function.

Step-by-step explanation:

In order to determine the region of the United States each city is located in, we can use the given criteria:

  1. A station is located in the 'Northeast' region if its latitude is above or equal to 40 degrees and its longitude is greater than or equal to -100 degrees.
  2. A station is located in the 'Northwest' region if its latitude is above or equal to 40 degrees and its longitude is less than -100 degrees.
  3. A station is located in the 'Southeast' region if its latitude is below 40 degrees and its longitude is greater than or equal to -100 degrees.
  4. A station is located in the 'Southwest' region if its latitude is below 40 degrees and its longitude is less than -100 degrees.

To add a new column in the cities labeled 'Region,' you can use the 'coordinates_to_region' function that you defined. This function will assign the appropriate region to each city based on its latitude and longitude coordinates.

User Tduchateau
by
8.9k points
4 votes

In order to add a new column labeled "Region" to your cities data, you can apply this function to each row of the cities dataframe.

import pandas as pd

# Assuming you have a DataFrame named 'cities' with columns 'Latitude' and 'Longitude'

# and you have a function named 'coordinates_to_region'

def coordinates_to_region(latitude, longitude):

# Implement the logic of your coordinates_to_region function

# ...

# Adding a new column 'Region' to the cities DataFrame

cities['Region'] = cities.apply(lambda row: coordinates_to_region(row['Latitude'], row['Longitude']), axis=1)

User Pravin Suthar
by
8.5k points