3-3-3-4. Creating pandas Series
In this lesson, we will go over the two main data structures in Pandas.
The Pandas series and the Panda’s dataframe.
Let’s start off by learning about the Pandas series object, and how to create one.
When importing Pandas, use a standard alias, pd.
Let’s create a series containing grocery items.
To access the series object,
we just type pd.Series with a capital S.
A panda series is a one-dimensional array-like object that can hold many data types,
such as numbers and strings.
This is different from a NumPy array which can only hold one data type.
Another big difference between a Pandas series and
a NumPy array is that you can assign an index label to each element in the Pandas series.
Here, we pass two arguments,
the data and the indices.
For our grocery series and we will use food names as
index labels and the quantities we need to buy as our data.
We can see that a Pandas series is displayed with the indices in the first column,
and the data in the second column.
Notice that the data is not indexed zero to three,
but rather with the names of the foods that we put in,
eggs, apples, milk, and bread.
Also notice that the data in our Pandas series has both integers and strings,
just like NumPy arrays.
Pandas series have attributes that allow us to get
information from them in an easy way. Let’s see some of them.
Shape gives us the sizes of each dimension of the data,
ndim gives us the number of dimensions of the data,
and size gives us the total number of values in the array.
We can also print the index labels and the data of the Pandas series separately.
This is useful if you don’t happen to know what the index labels of a series are.
This gives us the index labels of the series object,
and this gives us the data into series object.
If you’re dealing with a very large Pandas series,
and you’re unsure whether an index label exists,
you can always check using the In command.
This let’s us know that bananas is not one of the index labels in the grocery series,
and this tells us that bread is.
Pandas는 데이터 분석 및 조작을 위한 강력한 도구입니다.
지난 수업을 기억한다면,
이 패키지는 NumPy를 기반으로 구축되어 매우 빠르고 효율적입니다.
이 수업에서는 Pandas의 두 가지 주요 데이터 구조를 살펴보겠습니다.
Pandas 시리즈와 Panda의 데이터 프레임.
Pandas 시리즈 객체와 생성 방법에 대해 알아보는 것으로 시작해 보겠습니다.
Pandas를 가져올 때 표준 별칭인 pd를 사용합니다.
식료품 항목이 포함된 시리즈를 만들어 보겠습니다.
시리즈 개체에 액세스하려면
대문자 S로 pd.Series를 입력하면 됩니다.
팬더 시리즈는 많은 데이터 유형을 보유할 수 있는 1차원 배열과 유사한 객체입니다.
숫자 및 문자열과 같은.
이것은 하나의 데이터 유형만 보유할 수 있는 NumPy 배열과 다릅니다.
Pandas 시리즈와 다른 큰 차이점은
NumPy 배열은 Pandas 시리즈의 각 요소에 인덱스 레이블을 할당할 수 있다는 것입니다.
여기에서 두 개의 인수를 전달합니다.
데이터와 인덱스.
식료품 시리즈의 경우 식품 이름을 다음과 같이 사용합니다.
인덱스 레이블과 데이터로 구매해야 하는 수량.
Pandas 시리즈가 첫 번째 열에 인덱스와 함께 표시되는 것을 볼 수 있습니다.
두 번째 열의 데이터입니다.
데이터가 0에서 3으로 인덱싱되지 않음에 유의하십시오.
오히려 우리가 넣는 음식의 이름으로,
계란, 사과, 우유, 빵.
또한 Pandas 시리즈의 데이터에는 정수와 문자열이 모두 있습니다.
NumPy 배열처럼.
Pandas 시리즈에는 다음을 얻을 수 있는 속성이 있습니다.
그들로부터 쉽게 정보를 얻을 수 있습니다. 그 중 몇 가지를 살펴보겠습니다.
모양은 데이터의 각 차원의 크기를 제공합니다.
ndim은 데이터의 차원 수를 제공합니다.
크기는 배열의 총 값 수를 제공합니다.
Pandas 시리즈의 인덱스 레이블과 데이터를 별도로 인쇄할 수도 있습니다.
이것은 시리즈의 인덱스 레이블이 무엇인지 모르는 경우에 유용합니다.
이것은 시리즈 객체의 인덱스 레이블을 제공합니다.
이것은 우리에게 데이터를 시리즈 객체로 제공합니다.
매우 큰 Pandas 시리즈를 다루는 경우
색인 레이블이 있는지 확실하지 않은 경우
In 명령을 사용하여 항상 확인할 수 있습니다.
이것은 바나나가 식료품 시리즈의 색인 레이블 중 하나가 아니라는 것을 알려줍니다.
그리고 이것은 우리에게 빵이 있음을 알려줍니다.
Pandas Series
A Pandas series is a one-dimensional array-like object that can hold many data types, such as numbers or strings, and has an option to provide axis labels.
Difference between NumPy ndarrays and Pandas Series
- One of the main differences between Pandas Series and NumPy ndarrays is that you can assign an index label to each element in the Pandas Series. In other words, you can name the indices of your Pandas Series anything you want.
- Another big difference between Pandas Series and NumPy ndarrays is that Pandas Series can hold data of different data types.
Let’s start by importing Pandas into Python. It has become a convention to import Pandas as pd
, therefore, you can import Pandas by typing the following command in your Jupyter notebook:
import pandas as pd
Let’s begin by creating a Pandas Series. You can create Pandas Series by using the command pd.Series(data, index)
, where index
is a list of index labels. Let’s use a Pandas Series to store a grocery list. We will use the food items as index labels and the quantity we need to buy of each item as our data.
Example 1 – Create a Series
# We import Pandas as pd into Python import pandas as pd # We create a Pandas Series that stores a grocery list groceries = pd.Series(data = [30, 6, 'Yes', 'No'], index = ['eggs', 'apples', 'milk', 'bread']) # We display the Groceries Pandas Series groceries
eggs 30 apples 6 milk Yes bread No dtype: object
We see that Pandas Series are displayed with the indices in the first column and the data in the second column. Notice that the data is not indexed 0 to 3 but rather it is indexed with the names of the food we put in, namely eggs, apples, etc… Also, notice that the data in our Pandas Series has both integers and strings.
Just like NumPy ndarrays, Pandas Series have attributes that allow us to get information from the series in an easy way. Let’s see some of them:
Example 2 – Print attributes – shape, ndim,and size
# We print some information about Groceries print('Groceries has shape:', groceries.shape) print('Groceries has dimension:', groceries.ndim) print('Groceries has a total of', groceries.size, 'elements')
Groceries has shape: (4,) Groceries has dimension: 1 Groceries has a total of 4 elements
We can also print the index labels and the data of the Pandas Series separately. This is useful if you don’t happen to know what the index labels of the Pandas Series are.
Example 3 – Print attributes – values, and index
# We print the index and data of Groceries print('The data in Groceries is:', groceries.values) print('The index of Groceries is:', groceries.index)
The data in Groceries is: [30 6 ‘Yes’ ‘No’] The index of Groceries is: Index([‘eggs’, ‘apples’, ‘milk’, ‘bread’], dtype=’object’)
If you are dealing with a very large Pandas Series and if you are not sure whether an index label exists, you can check by using the in
command
Example 4 – Check if an index is available in the given Series
# We check whether bananas is a food item (an index) in Groceries x = 'bananas' in groceries # We check whether bread is a food item (an index) in Groceries y = 'bread' in groceries # We print the results print('Is bananas an index label in Groceries:', x) print('Is bread an index label in Groceries:', y)
Is bananas an index label in Groceries: False Is bread an index label in Groceries: True
Additional Reading – Pandas Series Documentation
Refer to the Series documentation to have a quick glance at the different attributes of Series, and the optional arguments of the constructor.
댓글을 달려면 로그인해야 합니다.