Python技术迷

Pandas 教程-Pandas Series.to_frame()

整理:python架构师

系列被定义为一种可以容纳整数、字符串、双精度值等的列表类型。它返回一个对象形式的列表,该列表的索引从 0 到 n,其中 n 表示系列中值的长度。

系列和数据帧之间的主要区别在于,系列只能包含一个具有特定索引的列表,而数据帧是由多个系列组合而成的,可以分析数据。

Pandas Series.to_frame() 函数用于将系列对象转换为 DataFrame。

语法

Series.to_frame(name=None)

参数

name: 指对象。默认值为 None。如果有一个值,传递的名称将替代系列名称。

返回值

它返回系列的 DataFrame 表示形式。

专属福利 
👉点击领取:最全Python资料合集

示例1

s = pd.Series(["a", "b", "c"],  name="vals")  s.to_frame()
输出
       vals0          a1          b2          c

示例2

import pandas as pd   import matplotlib.pyplot as plt     emp = ['Parker', 'John', 'Smith', 'William']   id = [102, 107, 109, 114]     emp_series = pd.Series(emp)   id_series = pd.Series(id)     frame = { 'Emp': emp_series, 'ID': id_series }   result = pd.DataFrame(frame)     print(result)
输出
      Emp       ID0   Parker     1021   John        1072   Smith      1093   William   114
Image
 1
Image
热门推荐

Image