Course 3.2Lesson 1 of 5
pandas is the library for tables of data. A Series is a NumPy array with a label for each value, so it does arithmetic on all values, looks up a value by its label, and has methods such as mean and idxmax, and a comparison picks the labels that pass. A DataFrame is a table of columns of the same length, each with a name and a type, made for example from a dictionary of lists. df.shape gives the rows and columns, df.columns the names, df.dtypes the types, df[name] one column as a Series, df[list of names] a smaller table, df.head the first rows, df.iloc a row by its place, and df.describe the count, mean, spread and quartiles of every numerical column. Columns of text have the type str.
Try the code and the simulations beside the lesson.
After this lesson you can
In chapter 1, you worked with NumPy arrays: columns of numbers of one type. Real data is a table. Each column has a name (distance, period, mass), the columns have different types (names are text, distances are decimals), and some values are missing. A spreadsheet holds such a table. The library for tables in Python is pandas. It is built on NumPy, so everything that you know about arrays works inside it, and it adds names, labels, missing values and the tools to read, clean, join and summarise data. Almost every data analysis in science and business is done with it.
On this site, pandas is ready. The first time that your code uses it, the site needs a few seconds to load it. By custom we import it as pd:
A Series is a one-dimensional array in which every value has a label.
Predict first. Which line will print 1.0? What will period * 365.25 give? Then run it.
The printed Series shows the labels on the left, the values on the right, and the type of the values at the bottom: float64.
A DataFrame is a table made of Series that share the same index: columns of the same length, each with a name.
The bars are drawn to fit: the distances are stretched three times. In both columns Saturn, the sixth planet, is the largest of these six.
describe is a quick way to check a table for mistakes. A minimum of for a distance, or a count smaller than the number of rows, shows a problem.
df[["a", "b"]].ValueError.df["a"] and df.a. The first always works. The second fails if the name has a space.iloc for a label. iloc is by place, and loc is by label.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
df.shape, df.columns, df.dtypes, df.head() and df.describe() give a first look.df["a"] is a column. df[["a", "b"]] is a smaller table. df.iloc[i] is a row by place.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
name and mass of the table df?period.idxmax() give?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.