迹忆客 专注技术分享

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

Pandas DataFrame.to_dict()函数

作者:迹忆客 最近更新:2024/04/21 浏览次数:

Python Pandas DataFrame.to_dict() 函数将给定的 DataFrame 转换为字典。


pandas.DataFrame.to_dict() 的语法

DataFrame.to_dict(orient='dict',
                  into= < class 'dict' >)

参数

orient 这个参数决定了字典的类型。例如,它可以是一个系列或列表的字典。它有六个选项。它们是 dictlistseriessplitrecordsindex
into 它是一个类参数。我们可以传递一个实际的类或其实例作为参数。

返回

它返回代表传递的 Dataframe 的字典。


示例代码:DataFrame.to_dict() 方法将 DataFrame 转换为字典的字典

为了将 DataFrame 转换为字典的字典,我们将不传递任何参数。

import pandas as pd

dataframe=pd.DataFrame({'Attendance': {0: 60, 1: 100, 2: 80,3: 78,4: 95},
                        'Name': {0: 'Olivia', 1: 'John', 2: 'Laura',3: 'Ben',4: 'Kevin'},
                        'Obtained Marks': {0: 90, 1: 75, 2: 82, 3: 64, 4: 45}})
print("The Original Data frame is: \n")
print(dataframe)

dataframe1 = dataframe.to_dict()
print("The Dictionary of Dictionaries is: \n")
print(dataframe1)

输出:

The Original Data frame is: 

   Attendance    Name  Obtained Marks
0          60  Olivia              90
1         100    John              75
2          80   Laura              82
3          78     Ben              64
4          95   Kevin              45
The Dictionary of Dictionaries is: 

{'Attendance': {0: 60, 1: 100, 2: 80, 3: 78, 4: 95}, 'Obtained Marks': {0: 90, 1: 75, 2: 82, 3: 64, 4: 45}, 'Name': {0: 'Olivia', 1: 'John', 2: 'Laura', 3: 'Ben', 4: 'Kevin'}}

该函数返回了字典的字典。


示例代码:DataFrame.to_dict() 将 DataFrame 转换为 Series 字典的方法

要将一个 DataFrame 转换为 Series 的字典,我们将传递 series 作为 orient 参数。

import pandas as pd

dataframe=pd.DataFrame({'Attendance': {0: 60, 1: 100, 2: 80,3: 78,4: 95},
                        'Name': {0: 'Olivia', 1: 'John', 2: 'Laura',3: 'Ben',4: 'Kevin'},
                        'Obtained Marks': {0: 90, 1: 75, 2: 82, 3: 64, 4: 45}})
print("The Original Data frame is: \n")
print(dataframe)

dataframe1 = dataframe.to_dict('series')
print("The Dictionary of Series is: \n")
print(dataframe1)

输出:

The Original Data frame is: 

   Attendance    Name  Obtained Marks
0          60  Olivia              90
1         100    John              75
2          80   Laura              82
3          78     Ben              64
4          95   Kevin              45
The Dictionary of Series is: 

{'Attendance': 0     60
1    100
2     80
3     78
4     95
Name: Attendance, dtype: int64, 'Obtained Marks': 0    90
1    75
2    82
3    64
4    45
Name: Obtained Marks, dtype: int64, 'Name': 0    Olivia
1      John
2     Laura
3       Ben
4     Kevin
Name: Name, dtype: object}

函数返回 Series 字典。

转载请发邮件至 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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便