Python Tutorial: DataFrames and their methods

Learn how to explore and organize Pandas DataFrames with four essential methods. .head() previews rows, .info() shows column names and data types, .describe() calculates statistics like mean and max, and .sortvalues() reorders data by any column.
Chapters
In just three lines of code, you're already looking at your data represented in Python as a Pandas dataframe. For you, this should look similar to spreadsheet data. At the top of the data frame, we have our column names. Note each column is one unique data type. The price column here is numeric. We can expect to perform mathematical operations on it later. The color column is populated with text entries. Each row in this dataframe is a specific observation of a fruit's name, color, and price in US Dollars. Finally, on the left is the data frame's index. The index is a powerful component of the Pandas data frame, but beyond the scope of this course. Moving forward, I'll explicitly share when we're performing an action to avoid working with the index. Just like we used the dot to access functions in the Pandas package, like PD readexcel, we use the dot to access methods associated with data frames. Dataframe methods are like functions but accessed from within our dataframe. Object with the dot let's take a closer look at each of these common dataframe methods. The HEAD method allows us to look at the first few rows of our data frame. It's very useful for when you have hundreds or thousands of rows of data but only want to look at the first few. By default, this method will display the first five rows of our data frame. You can see on the left in our last line of code. We access this method by writing FRUIT head followed by a set of parentheses. We place FRUIT head inside the print function so our results will display in the console. We can pass an optional argument to the HEAD method if we wish to display an alternative number of rows. Here we pass a 2 in order to display the first two rows of data. The DOT info method provides us with details on the number of entries or rows in our dataframe, the total number of columns, the name of each column, and the data type of each column. Here on the right, we can see that Our DataFrame has 8 rows, 3 columns, 2 columns with an object or text data type, and 1 column with a float 64 or numerical data type. Int 64 is another common numerical data type. In short, int 64 represents whole numbers and float 64 represents signifies numbers with decimal places. The.describe method provides us with summary statistics for any numerical column in our dataframe. Here we see the mean or average price for fruit in our data is around 2.28 and the max price is 5.27. Finally, the sortvalues method allows us to rearrange the rows in our dataframe based on a column Here. We've used sortvalues to alphabetize our dataframe according to the name column in the code. You will also notice We've used the ResetIndex method. This is done so that our index remains ordered in the exercises. This will be done for you. We can also sort values in descending order by passing ascending false to the sortvalues method. This code chunk outputs a data frame of the most expensive fruits in our data set. Also note how we keep redefining fruit. First fruit equals the data we load in from our file, then fruit equals the data sorted by price, and so on. Now it's your turn to put some methods to work.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







