Course 3.2Lesson 3 of 5
A new column is made by a formula on whole columns, such as the period squared divided by the distance cubed for Kepler's third law, whose value is about 1 for every planet when the period is in years and the distance in astronomical units, and NumPy functions work on columns. groupby splits the rows by the values of a column, summarises each group with mean, sum, max or agg, and combines the results in a table, and value_counts counts the rows of each value. pd.merge joins two tables on a common column: the default keeps only the names that are in both tables, and how equal outer keeps all of them with NaN where a table has nothing. These three steps, derive, group and join, are the core of data analysis.
Try the code and the simulations beside the lesson.
After this lesson you can
groupby, andpd.merge.In chapter 1, you wrote a formula once and used it on a whole array. A column of a table works in the same way, and a new column is made by assigning to a new name.
A good example is Kepler's third law of planetary motion. Johannes Kepler found, after years of work with the data of Tycho Brahe, that for the planets of the Sun, the square of the period is proportional to the cube of the distance . If is in years and is in astronomical units (AU, the distance of the Earth from the Sun), then for every planet.
Predict first. For the Earth, and , so the ratio is 1. What do you expect for the other planets?
The ratios are 0.971, 1.030, 1.000, 1.006 and 1.000, and the mean is 1.002. Kepler's law is true to within a few percent, even for these rounded numbers. (The ratio for Mercury is the farthest from 1, and the reason is that the table gives Mercury's numbers with only two digits.)
A very common question is: how do the groups compare? The mean mass of the rocky planets and the giant planets, the average of a measurement for each trial, the number of patients in each ward. In pandas, this is groupby: split the rows into groups, apply a summary to each group, and combine the results.
It is worth seeing how the average hides the shape of the data. The mean of the giants, 111, is far from every one of them but Jupiter (317.8) and Saturn (95.2): Uranus has 14.5 and Neptune 17.1. A mean says little about a group with such different members.
Data is often in several tables. One table has the periods, and another has the masses, and the two share the column name. To use them together, we merge them.
The choice is again yours: do you want only the complete rows, or all the rows? The outer join shows you what is missing, which can be a good thing to know.
After an outer merge, the rows that were in only one table have missing values (NaN) in the other table's columns.
groupby("kind")["mass"] and groupby("mass")["kind"]. The first is the group, and the second is the column to summarise."Earth " with a space is not "Earth".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["c"] = df["a"] / df["b"].groupby(column)[other].mean() splits, summarises and combines. agg gives several summaries.pd.merge(a, b, on=column) joins two tables. how="outer" keeps all the rows.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
density that is mass divided by volume?df.groupby("kind")["mass"].max() 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.