Video instructions and help with filling out and completing Why Form 2220 Columns

Instructions and Help about Why Form 2220 Columns

Hello and welcome back to my Q&A video series about the pandas library in Python and the question for today comes from a YouTube commenter who asks can you make a video dedicated in pointing out the differences between lok i lok and IX okay great questions so Lok I Lok and IX are different data frame methods for selecting rows and columns and they're very flexible and powerful so it's important to understand all three of them so let's jump right in and out and use an example data set to explore these methods so we'll import pandas as PD and then we're going to use a data set of UFO reported sightings so we'll say UFO equals PD read CSV and then bit dot Lee slash UFO reports okay you can always follow along with this at home that's a public data set okay so if you wanted to look at the first few rows the your natural inclination might be to use the head method so if we want to see the first three rows UFO dot head three and that will show us those first three rows okay but there are lots of other ways to do this and Lok is one of them and it's a lot more powerful and flexible so let's try it out okay so um Lok is because of the data frame method you say UFO dot Lok and Lok is for filtering rows and selecting columns by label and by label I mean for rows I mean the index for columns I mean the column names okay that's the label soloq is for selecting things by label okay so you don't use parenthesis you use a bracket and the format of the Lok Command is what rows do I want , what columns do I want okay so if I want row zero and all columns I say this UFO dot look bracket 0 comma colon so the colon means all columns so row 0 all columns we run that and we actually get a backup pandas series which is just showing us this first row of the data frame so row 0 meaning this one all columns okay now what if I wanted multiple multiple rows okay I could say UFO dot Lok and what rows do I want let's say I want rows 0 1 & 2 I could actually just pass a list 0 comma 1 comma 2 and then outside of those brackets comma colon so which rows do I want 0 1 2 which columns do I want all of them okay and here we go now ah if you there's actually a more efficient way because we want this continuous block of rows okay I could say you can actually just use UFO dot Lok Lin 2 comma colon so which rows do I want 0 through 2 and I want all columns so I run that get the same thing okay so this is what you would use when you're when you want a block of them together but you can use the list Music format you know I could have put 0 comma 1 comma 3 and gotten those rows okay now you'll notice that when I use this notation its inclusive on both sides it's 0 1 & 2 it does not exclude the two like it would with rain change okay so that's really important to remember that Lok is inclusive on both sides when you use this notation okay now I will mention I'm not going to recommend you do this but I will mention that sometimes you'll see code like this and you'll think wait a minute I thought it was Rose comma columns and that's true but it turns out if you leave off the you know comma colon pandas just assumes it and it will return the same result now I'm going to ask you or recommend that you don't do that because as the Python philosophy is explicit is better than implicit so even though this might look a little nicer just do a favor for the person reading your code and show them okay I want these rows I want all columns okay now that's for doing a row selection what about column selection it's just like you might expect okay so we're going to say UFO dot Lok and first we're going to start with all rows column City okay and we get back the city series okay all rows what column City and as before if we wanted multiple columns we could do something like this I could say I want city and state so which rows all of them which columns city and state and we get that now we get to eat a frame with two columns or I can instead specify it as a range I want city through state all rows city through state so City colors reported shape reported state okay so arm you can of course combine these two in terms of row selection and column selection so if I want rows zero through two and columns city through state I can do it that way okay so very flexible and I'd ask you real quick what's another way we could do this this exact same thing without using the loke method think about that for a second pause the video if you need but if you remember back to previous videos you could accomplish the same thing with UFO dot head three and then dot drop time axis equals one okay so same thing totally different kind of workflow um the point here is there are always lots of ways to do the same thing in pandas and it comes down to what methods you feel most comfortable with and you know the best I'll just say that dot lok is super powerful and flexible so okay um there are there is another use