《Python数据分析技术栈》第06章使用 Pandas 准备数据 02 pandas的组件(Building blocks of Pandas)

发布时间:2024年01月22日

02 pandas的组件(Building blocks of Pandas)

《Python数据分析技术栈》第06章使用 Pandas 准备数据 02 pandas的组件(Building blocks of Pandas)

The Series and DataFrame objects are the underlying data structures in Pandas. In a nutshell, a Series is like a column (has only one dimension), and a DataFrame (has two dimensions ) is like a table or a spreadsheet with rows and columns. Each value stored in a Series or a DataFrame has a label or an index attached to it, which speeds up retrieval and access to data. In this section, we learn how to create a Series and DataFrame, and the functions used for manipulating these objects.

系列(Series)和数据帧(DataFrame)对象是 Pandas 的底层数据结构。简而言之,Series 就像一列(只有一个维度),而 DataFrame(有两个维度)就像一个有行和列的表格或电子表格。存储在系列或 DataFrame 中的每个值都附有标签或索引,这可以加快数据检索和访问速度。在本节中,我们将学习如何创建系列和数据帧,以及用于操作这些对象的函数。

Creating a Series object

The Series is a one-dimensional object, with a set of values and their associated indexes. Table 6-1 lists the different ways of creating a series.

系列是一个一维对象,包含一组值及其相关索引。表 6-1 列出了创建系列的不同方法。

Using a scalar value:

使用标量值:

pd.Series(2)
#Creating a simple series with just one value. Here, 0 is the index label, and 2 is the value the Series object contains.

Using a list:

pd.Series([2]*5)
#Creating a series by enclosing a single value (2) in a list and replicating it 5 times. 0,1,2,3,4 are the autogenerated index labels.

Using characters in a string:

pd.Series(list('hello'))
#Creating a series by using each character in the string "hello" as a separate value in the Series.

Using a dictionary:

pd.Series({1:'India',2:'Japan',3:'Singapore'})
#The key/value pairs correspond to the index labels and values in the Series object.

Using a range:

pd.Series(np.arange(1,5))
#Using the NumPy arrange function to create a series from a range of 4 numbers (1-4), ensure that the NumPy library is also imported

Using random numbers:

pd.Series(np.random.normal(size=4))
#Creating a set of 4 random numbers using the np.random.normal function

Creating a series with customized index labels:

pd.Series([2,0,1,6],index=['a','b','c','d'])
#The list [2,0,1,6] specifies the values in the series, and the list for the index['a','b','c','d'] specifies the index labels

To summarize, you can create a Series object from a single (scalar) value, list, dictionary, a set of random numbers, or a range of numbers. The pd.Series function creates a Series object (note that the letter “S” in “Series” is in uppercase; pd.series will not work). Use the index parameter if you want to customize the index.

简而言之,您可以通过单个(标量)值、列表、字典、随机数集或数字范围创建 Series 对象。函数 pd.Series 将创建一个 Series 对象(注意,"Series "中的字母 "S "是大写字母;pd.series 将不起作用)。如果要自定义索引,请使用索引参数。

文章来源:https://blog.csdn.net/qq_37703224/article/details/135739016
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。