I’m continuing to break down a very popular stackoverflow post at https://stackoverflow.com/questions/17071871/select-rows-from-a-dataframe-based-on-values-in-a-column-in-pandas.
df.loc[(df['column_name'] >= A) & (df['column_name'] <= B)]
df‘s in the line of code above represent the name of data frame that we are working with.
.loc is a keyword.
The outermost square brackets
[...] contains the condition that you want to use as filter. In this case, we have two conditions inside separated by
& with the each condition enclosed by an open and close parenthesis
This is condition #1:
(df['column_name'] >= A)
This is condition #2:
(df['column_name'] <= B)
The column_name in condition #1 can be the same or different from condition #2; it all depends on how you want to filter the data frame. The same goes for the
<= comparison signs, and
Suppose I have a data frame called
my_tie_collection. If I only want ties that are both blue and made from silk, then, I would type:
my_tie_collection.loc[(my_tie_collection['color'] == 'blue') & (my_tie_collection['material'] == 'silk')]
Take note, I used
== to denote matching and I also put single quotes around
Today, I’ll be breaking down a very popular stackoverflow post at https://stackoverflow.com/questions/17071871/select-rows-from-a-dataframe-based-on-values-in-a-column-in-pandas.
df.loc[df['column_name'] == some_value]
df is the name of the data frame. You should replace it twice with the name of your data frame.
.loc is a keyword.
column_name with the name of the column that contains the values you want to filter.
some_value with the desired value.
For example, if I have a data frame named “my_shoe_collection” and I want to select only the rows where the value of “color” is “blue” then:
my_shoe_collection.loc[my_shoe_collection['color'] == 'blue']
Also, if I have the same data frame named and I want to only select rows where the value of “price” is less than $50, then:
my_shoe_collection.loc[my_shoe_collection['price'] <= 50]
Notice how I got rid of the single quotation marks since I’m dealing with an actual number?
There’s a saying while I attended military bootcamp back in the day:
The days seem to drag on endlessly but weeks fly by…
Well this week we’re doing practice interviews and I can’t help but wonder where the last few months had gone.
Seemed like only yesterday, when I had started this blog with the intention of documenting my experience at Codeup. Didn’t quite pan out as planned but did I have a blast!
Yes it was intense but it was hella fun. Stressful, but exciting at the same time.
Now I can actually see the finish line.
We graduate on June 14th!