uky-transport-data-science / ce599

CE599-002 Data Science for Transportation
22 stars 62 forks source link

Working with loops to index number of bikes per boro #6

Closed jdbr223 closed 7 years ago

jdbr223 commented 7 years ago

Right now I have a function that takes an open dataframe and allows me to individually index the number of bikes from the df dataframe based on whether the bool for each boro is true or false. My logic works if I just pass integers, but not for when I want to loop. Below is my code:

Function to create boolean columns in df dataframe

def bike_in_boro(x):
boro = boros.geometry[x] in_boro = df.geometry.within(boro) return in_boro

Creates columns

for j in range(0,5): if j == 0: b = bike_in_boro(j) df['Statisland']=b elif j == 1: b = bike_in_boro(j) df['Brooklyn']=b elif j == 2: b = bike_in_boro(j) df['Queens']=b elif j == 3: b = bike_in_boro(j) df['Manhattan']=b elif j == 4: b = bike_in_boro(j) df['Bronx']=b

Creates a new bikes dataframe to carry bike values:

bikes = pd.DataFrame(index = range(667),columns = ['Statisland','Brooklyn','Queens','Manhattan','Bronx']) bikes.head()

What Works. Also, df is the same dataframe that was created in the citibike tutorial. It has already had rows added to where if the bike location is within a given boro, it is true and if not it is false.

if df.iloc[0,19] == True: bikes.iloc[0,0] = df.iloc[0,1] else: bikes.iloc[0,0] = [0]

My Function

def bike_index(r,k): if df.iloc[r,k+19] == True: bikes.iloc[r,k] = df.iloc[r,1] else: bikes.iloc[r,k] = 0

What I want to do

for k in range (0,5): for x in range (0,667): bike_index(r,k)

When I pass values through the equation in a loop, I always get an index positioning error. Any help is appreciated.

gregerhardt commented 7 years ago

I'm guessing the indices between 19 and 685 does not exist. Can you link to your notebook (or a stand-alone notebook with just this example) so I can try it?

jdbr223 commented 7 years ago

Sure. Here's the link: http://localhost:8888/notebooks/11-Spatial%20Analysis/citibike.ipynb

gregerhardt commented 7 years ago

You only have 666 rows, not 667.

jdbr223 commented 7 years ago

That took care of it. Thanks! It's always something simple isn't it?