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

51 篇文章 0 订阅
46 篇文章 0 订阅

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 将不起作用)。如果要自定义索引,请使用索引参数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Python私教

创业不易,请打赏支持我一点吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值