In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to use the Pandas sequence, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!
Pandas sequence (Pandas Series)
Pandas is an open source BSD (Berkeley Software Distribution) license library that provides high-performance, easy-to-use data structures and data analysis tools for the Python programming language.
There are two different types of data structures provided by Pandas:
Pandas data Framework (Pandas DataFrame)
Pandas sequence
We will introduce Pandas sequences in this article.
Note: it is recommended that you read the previous post about Pandas DataFrame before continuing with this article to better understand the Pandas sequence.
Https://towardsdatascience.com/pandas-dataframe-a-lightweight-intro-680e3a212b96
What is the Pandas sequence?
Technically, a Pandas sequence is an one-dimensional tag array that can hold any data type.
In popular terms, the Pandas sequence is just a column in the excel table. As shown in the following figure, such as the sequence of the person's name, age, and position.
Pandas sequence
Therefore, in the case of Pandas DataFrame, the Pandas sequence represents a single column in memory, which can be independent or belong to Pandas DataFrame.
Note: Pandas sequences can have their own independent existence and do not belong to Pandas DataFrame.
How to create a Pandas sequence?
You can use Python lists or NumPy arrays to create Pandas sequences. It is important to remember that unlike Python lists, Pandas sequences will always contain the same type of data. This makes NumPy arrays a better choice for creating Pandas sequences.
The following two methods are used to create Pandas sequences:
This is an example of them.
Result of → series_list = pd.Series ([1, 2, 3, 4, 5, 6])
Result of → series_np = pd.Series (np.array)
Just like when you create a Pandas DataFrame, the Pandas sequence generates a row index number by default, which is a series of incremental numbers starting at 0.
You may have guessed that you can have your own row index value when creating a Pandas sequence. All we need to do is pass index parameters, which take the same type of list or NumPy array.
The following example uses a sequence generated by NumPy:
Result of → series_index = pd.Series (np.array ([10
The following example uses a string as the row index:
Result of → series_index = pd.Series (np.array ([10
We can use the row index of the Pandas sequence as:
Whether or not we passed a list or NumPy array when we created the series, it returns the NumPy array
Create Pandas sequences from python Dictionary
As we saw when we created the Pandas DataFrame, it is very easy to create a DataFrame from the python dictionary because the key maps to the column name and the value corresponds to the column values list.
So how does it map when creating a Pandas sequence?
If we create a Pandas sequence from the python dictionary, the key becomes the row index and the value becomes the value of the row index.
For example, let's see what happens to a simple dictionary with a single key-value pair.
This is an example of the output
Result of → Code block Above
If the values in the dictionary contain a list of items, the situation will not change. List items are still part of a single-row index
Result of → series_dict = pd.Series (t_dict)
Get Pandas sequence from Pandas DataFrame
Although Pandas sequences themselves are useful for data analysis and provide many useful helper functions, in most cases, analysis requirements will force us to use Pandas DataFrame and Pandas sequences together.
Let's first create a Pandas DataFrame, just like we created here:
The following is an example of the generated DataFrame
The result of creating a DataFrame from a dictionary
DataFrame provides two ways to access columns, using dictionary syntax df ['column_name'] or df.column_name. Every time we use these representations to get a column, we get a Pandas sequence. In the above example, we can get the Pandas sequence (that is, a single column) by accessing the column.
Pandas sequence name
Pandas sequence age
Pandas sequence position
Get the Pandas sequence by iterating through the columns of DataFrame
What if we don't know the name of the column?
Pandas DataFrame is iterable, and we can iterate through each column to get the Pandas sequence.
Create a DataFrame using Pandas sequences (stand-alone or combined)
A Pandas DataFrame is just a collection of sequences (1 +). We can use a single Pandas sequence or combine multiple Pandas sequences to generate DataFrame
For example, let's generate a DataFrame from a combination of series_name and series_age:
Surprisingly, the generated DataFrame should look like
Df_from_series
Yes, the row index of the Pandas sequence becomes the column, and the column becomes the row index value. You can think of this as similar to the transpose of a matrix. This is true even if we provide a single Pandas sequence to create the DataFrame:
Df_from_series_single
However, this does not happen when we remove the list / array representation from the Pandas sequence. For example
Will cause the column names and row indexes of the Pandas sequence to be preserved
Df_from_series
Note: unfortunately, this is limited to one sequence, because DataFrame API does not bring multiple parameters to the sequence.
Create a DataFrame behavior using Python Dict
When we pass python dictionaries as an array to create a DataFrame, we will observe the same behavior. Let's take a look at the previously created t_dict = {'axiaqizuzhuajiaoguanghuanjinghuanghuanghuanghuanghuanghuanghuanghuanzhongzhuangzhuangzhuangzhu Zhi
As a result, DataFrame looks like
Ds
Where the key is represented as a column, otherwise if we create a sequence, it is represented as a row index.
We can even combine multiple t_dict to create DataFrame
Ds
Sequence auxiliary function
Just like pandas DataFrame, sequences have several sets of helper functions for data analysis.
Note that all column helper functions of Pandas DataFrame can be used with Pandas sequences. Some examples are
Iterative sequence
Like many other data structures in python, a simple for loop iteration sequence can be used
We can also iterate over the sequence rows of the index:
The above is all the content of this article "how to use Pandas sequence". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.