如何使用标量/常量值在 Python 中创建序列数据结构?
标量或常量值只定义一次,并在序列数据结构的所有行/条目中重复。以下是一个示例 -
示例
import pandas as pd
my_index = ['ab', 'mn' ,'gh','kl']
my_series = pd.Series(7, index = my_index)
print("This is series data structure created using scalar values and specifying index values")
print(my_series)输出
This is series data structure created using scalar values and specifying index values ab 7 mn 7 gh 7 kl 7 dtype: int64
解释
- 导入所需的库,并为其指定别名,以便于使用。
- 创建索引值的列表。
- 将常量值传递给 'pandas' 库中的 'Series' 函数。
- 同时,也传递索引列表。
- 然后在控制台上显示它。
如果未自定义索引值,则采用从 0 开始的默认值。由于只指定了一个常量值,因此序列数据结构中将只有一个条目。如下所示 -
示例
import pandas as pd
my_series = pd.Series(7)
print("This is series data structure created using scalar values and default index values")
print(my_series)输出
This is series data structure created using scalar values and default index values 0 7 dtype: int64
解释
导入所需的库,并为其指定别名,以便于使用。
将常量值传递给 'pandas' 库中的 'Series' 函数。
然后在控制台上显示它。
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP