Wednesday, December 4, 2019

DataFrames and Series

There are two important components of Pandas: DataFrames and Series . Series refers to a column of data, while a group of Series constitutes a DataFrame. Below is a series example for Toyota vehicles:



Below is a series example for BMW vehicles:



Below is a DataFrame for the two series examples above:



From the information above, we can deduce that Series and DataFrames share a lot of similarities. For this reason, most of the operations that you can perform with one of them can be performed on the other, too.

Building DataFrames

Learning how to build unique DataFrames in Python is a fundamental skill that will help you when testing functions and new methods in Pandas. There are several ways of creating DataFrames. The best method is always the simplest: using dict .

Using the data above, you can use Pandas to determine the department sales in a car dealership. You need to create a column for each model and a row for customer purchases. In order to have this organized as a Pandas dictionary, you will have the following code:

data = {
'Toyota': [3, 4, 1, 5],
'BMW': [4, 5, 2, 9]
}

The information above is then passed to the DataFrame constructor in Pandas as follows:

sales = pd.DataFrame (data)
sales

Output



How do we arrive at this output? Every data item (key, value) represents a column within the DataFrame. When you create the DataFrame, the index is determined as 0-3. Indices are determined when you create the DataFrame.

Alternatively, you can also create your own indices as shown:

sales = pd.DataFrame (data, index=[‘Hatchback’, ‘SUV’, ‘Sedan’,
‘Convertible’])
sales

Your output will be as follows:



From this information, you can determine the number of orders made for each vehicle type using the name. You do this by using loc (from locate) as shown below:

sales.loc['Hatchback']
Output:
Toyota 3
BMW 4
Model: Hatchback, dtype: int64

You can use this knowledge to create DataFrames for different data models you
are working on.
Share:

0 comments:

Post a Comment