/logo-square.png

Python Pandas For Your Grandpa - 3.5 DataFrame apply()

Just as Series has an apply() method for applying some function to each element in a Series, DataFrame has an apply() method that let’s you apply a function to each row or column in a DataFrame. In this video, we’ll see how and when to use it. We’ll start by making a very simple three-row, two-column DataFrame called df. import numpy as np import pandas as pd df = pd.

Python Pandas For Your Grandpa - 3.6 DataFrame View vs Copy

As with Series, when you work with DataFrames, it’s important to be aware of when you’re copying data and when you’re referencing data. In this section, we’ll see examples of both. To start, we’ll make a simple DataFrame called df. import numpy as np import pandas as pd df = pd.DataFrame({ 'x': [1, 2, 3], 'y': [10, 20, 30] }) print(df) ## x y ## 0 1 10 ## 1 2 20 ## 2 3 30 Now let’s create a variable v1 and set it equal to the Series extracted from column x of df.

Python Pandas For Your Grandpa - 3.7 DataFrame merge()

In this section, we’ll look at one of the most common and important operations regarding dataframes - merging them together. To do this in pandas, the workhorse function is merge(). To motivate it, suppose we’re doing some analysis for a veterinary office and the office has two tables of data pets which has one row per pet - here the index represents each pet_id. import numpy as np import pandas as pd pets = pd.

Python Pandas For Your Grandpa - 3.8 DataFrame Aggregation

In this section, we’ll see how you can aggregate the rows of a DataFrame to calculate summary statistics. For example, suppose we have a DataFrame with two columns of floats, x and y, and we want to calculate the sum of each column. import numpy as np import pandas as pd df = pd.DataFrame({ 'x': [3.1, 5.5, 9.2, 1.7, 1.2, 8.3, 2.6], 'y': [1.4, np.nan, 5.0, 5.8, 9.0, np.nan, 9.

Python Pandas For Your Grandpa - 3.9 DataFrame groupby()

In this section, we’ll see how you can use groupby() to partition a DataFrame into groups and subsequently aggregate or transform the data in each group. To start, let’s build a DataFrame with four columns, A, B, C, and D. import numpy as np import pandas as pd df = pd.DataFrame({ 'A': ['foo', 'bar', 'foo', 'bar', 'bar', 'foo', 'foo'], 'B': [False, True, False, True, True, True, True], 'C': [2.1, 1.

Python Pandas For Your Grandpa - 4.1 Strings

Earlier in the course I said a Pandas Series is like a souped up version of a NumPy 1-D array. Perhaps the best example of this is when you’re dealing with a Series of strings. In this video, we’ll look at Pandas methods for processing a Series of strings. Let’s start by building a Series of strings that we’ll call cajun. import numpy as np import pandas as pd cajun = pd.