接下来我们给 Series 给一个 name 参数,打印我们构造的 Series 可以看到它的基础信息里面多了一个 name: ser = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'], name='age' ) ser ''' a 1 b 2 c 3 d 4 Name: age, dtype: int64 ''' 这个name 是这个 Series 的名称,如...
size 获取Series中数据量 shape Series数据维度 dtype Series数据类型 Values 获取Series的数据 我们来看一下这些比较常见的方法: (1)loc import pandas as pd obj = pd.Series([1,2,3,4],index=['a','b','c','d']) print(obj['a']) print(obj['a':'c']) print(obj.loc['a']) print(obj...
s = pd.Series(['Tom','William Rick','John','Alber@t', np.nan,'1234','SteveMinsu'])print(s.str.len()) Python 执行上面示例代码,得到以下结果 - 0 3.0 1 12.0 2 4.0 3 7.0 4 NaN 5 4.0 6 10.0 dtype: float64 Shell 4. strip()函数示例 importpandasaspdimportnumpyasnp s = pd.Seri...
columns:命名列。 这里的参数data可以接受多种不同的形式:int、string、boolean、list、tuple、dictionary,等。 创建一个n×m大小的数据框架 让我们创建一个10行5列的数据框架,填充的值都为1。这里我们指定data=1,且有10行(索引)和5列。 图1 从列表中创建数据框架 从列表创建数据框架,开始可能会让人困惑,但一...
print(series_from_list) 2.2 从NumPy数组创建 9 1 2 3 4 5 6 importnumpyasnp importpandasaspd data=np.array([1,3,5,7,9]) series_from_np_array=pd.Series(data) print(series_from_np_array) 2.3 指定索引 9 1 2 3 4
但相较于map()针对单列Series进行处理,一条apply()语句可以对单列或多列进行运算,覆盖非常多的使用场景。 下面我们来分别介绍: 单列数据 这里我们参照2.1向apply()中传入lambda函数: 代码语言:javascript 代码运行次数:0 运行 AI代码解释 data.gender.apply(lambda x:'女性'ifx is'F'else'男性') ...
The string representation(代表) of a Series displaye interactively(交互地) show the index on the left and the value on the right.(索引显示在左边, 值在右边) Since we did not specify(指定) an index for the data, a default one consisting of the integer 0 throught N-1(where N is the ...
Series 和 DataFrame 数据对象的 reindex 方法可以对数据重新索引,数据分析程序获取的 数据可能会有很多缺失值,需要对这些数据重新建立索引,并且填充缺失值。 [例 4] 对数据重新索引 程序清单如下。 # 导入 pandas 库 import pandas as pd # 导入 NumPy 库 import numpy as np # 通过列表数据创建 s_data = pd...
to_string() 输出:正如我们在输出中看到的那样,Series.to_string()函数已经成功地为给定的对象呈现了一个字符串表示。示例2: 使用Series.to_string()函数渲染给定序列对象的字符串表示。# importing pandas as pd import pandas as pd # Creating the Series sr = pd.Series([19.5, 16.8, 22.78, 20.124, ...
read_pickle()、DataFrame.to_pickle()和Series.to_pickle()可以读取和写入压缩的 pickle 文件。支持gzip、bz2、xz、zstd的压缩类型用于读取和写入。zip文件格式仅支持读取,且必须只包含一个要读取的数据文件。 压缩类型可以是一个显式参数,也可以从文件扩展名中推断出来。如果是‘infer’,则在文件名以'.gz'、'...