迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Python >

Pandas Series.to_frame()函数

作者:迹忆客 最近更新:2023/03/20 浏览次数:

Python Pandas Series.to_frame() 函数将给定的 Series 转换为 DataFrame。

pandas.Series.to_frame() 语法

Series.to_frame(name=None)

参数

   
name 它是一个对象类型参数。它告诉我们,如果 Series 有名称,那么传递的名称将代替 Series 名称。

返回值

它返回一个代表 Series 的 DataFrame。

示例代码:Series.to_frame() 方法将 Series 转换为 DataFrame

import pandas as pd

series = pd.Series([ 'Rose', 'Jasmine', 'Lili', 'Tulip', 'Hibiscus', 'Sun Flower', 'Orchid', 'Carnation','Irises', 'Gardenias'])
print("The Original Series is: \n")
print(series)

dataframe = series.to_frame()
print("The Dataframe is: \n")
print(dataframe)

输出:

The Original Series is: 

0          Rose
1       Jasmine
2          Lili
3         Tulip
4      Hibiscus
5    Sun Flower
6        Orchid
7     Carnation
8        Irises
9     Gardenias
dtype: object
The Dataframe is: 

            0
0        Rose
1     Jasmine
2        Lili
3       Tulip
4    Hibiscus
5  Sun Flower
6      Orchid
7   Carnation
8      Irises
9   Gardenias

该函数返回了代表给定 Series 的 DataFrame。

示例代码:Series.to_frame() 方法将 Series 转换为具有特定列名的 DataFrame

import pandas as pd

series = pd.Series([ 'Rose', 'Jasmine', 'Lili', 'Tulip', 'Hibiscus', 'Sun Flower', 'Orchid', 'Carnation','Irises', 'Gardenias'])
print("The Original Series is: \n")
print(series)

dataframe = series.to_frame(name= 'Flowers')
print("The Dataframe is: \n")
print(dataframe)

输出:

The Original Series is: 

0          Rose
1       Jasmine
2          Lili
3         Tulip
4      Hibiscus
5    Sun Flower
6        Orchid
7     Carnation
8        Irises
9     Gardenias
dtype: object
The Dataframe is: 

      Flowers
0        Rose
1     Jasmine
2        Lili
3       Tulip
4    Hibiscus
5  Sun Flower
6      Orchid
7   Carnation
8      Irises
9   Gardenias

现在列名是 Flowers

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Pandas read_csv()函数

发布时间:2024/04/24 浏览次数:254 分类:Python

Pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 DataFrame 中。

Pandas 追加数据到 CSV 中

发布时间:2024/04/24 浏览次数:352 分类:Python

本教程演示了如何在追加模式下使用 to_csv()向现有的 CSV 文件添加数据。

Pandas 多列合并

发布时间:2024/04/24 浏览次数:628 分类:Python

本教程介绍了如何在 Pandas 中使用 DataFrame.merge()方法合并两个 DataFrames。

Pandas loc vs iloc

发布时间:2024/04/24 浏览次数:837 分类:Python

本教程介绍了如何使用 Python 中的 loc 和 iloc 从 Pandas DataFrame 中过滤数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便