📜  从字典词典创建 Pandas 数据框

📅  最后修改于: 2022-05-13 01:55:11.514000             🧑  作者: Mango

从字典词典创建 Pandas 数据框

在本文中,我们将讨论如何从Python中的字典字典创建 pandas 数据框。

方法一:使用DataFrame()

我们可以使用 Pandas.DataFrame() 方法创建一个数据框。

句法:

pandas.DataFrame(dictionary)

在哪里

  • pandas 是支持 DataFrame 数据结构的模块
  • DataFrame是将字典转换为数据帧的数据结构
  • 字典是输入字典

示例:从字典中创建 pandas 数据框。

Python3
# import pandas module
import pandas
  
# create student dictionary of dictionaries 
# with 3 students with Age and address
data = {'Ojaswi': {'Age': 15, 'Address': 'Hyderabad'},
        'Rohith':  {'Age': 9, 'Address': 'Hyderabad'},
        'Gnanesh':  {'Age': 15, 'Address': 'Guntur'}}
  
# create pandas dataframe from dictionary of 
# dictionaries
data = pandas.DataFrame(data)
  
# display
print(data)


Python3
# import pandas module
import pandas
  
# create student dictionary of dictionaries with
# 5 students with Age ,subject and  address
data = {'Ojaswi': {'Age': 15, 'subject': 'java', 'Address': 'Hyderabad'},
        'Rohith':  {'Age': 9, 'subject': 'python', 'Address': 'Hyderabad'},
        'Gnanesh':  {'Age': 15, 'subject': 'c/c++', 'Address': 'Guntur'},
        'divya':  {'Age': 21, 'subject': 'html', 'Address': 'ponnur'},
        'ramya':  {'Age': 15, 'subject': 'c/c++', 'Address': 'delhi'}}
  
# create pandas dataframe from dictionary of
# dictionaries
data = pandas.DataFrame(data)
  
# display
data


Python3
# import pandas module
import pandas
  
# create student dictionary of dictionaries with 3 
# students with Age and address
data = {'Ojaswi': {'Age': 15, 'Address': 'Hyderabad'},
        'Rohith':  {'Age': 9, 'Address': 'Hyderabad'},
        'Gnanesh':  {'Age': 15, 'Address': 'Guntur'}}
  
# create pandas dataframe from dictionary of dictionaries 
# using from_dict() method
data = pandas.DataFrame.from_dict(data)
  
# display
data


输出:

示例 2:示例:从字典中创建 pandas Dataframe。

Python3

# import pandas module
import pandas
  
# create student dictionary of dictionaries with
# 5 students with Age ,subject and  address
data = {'Ojaswi': {'Age': 15, 'subject': 'java', 'Address': 'Hyderabad'},
        'Rohith':  {'Age': 9, 'subject': 'python', 'Address': 'Hyderabad'},
        'Gnanesh':  {'Age': 15, 'subject': 'c/c++', 'Address': 'Guntur'},
        'divya':  {'Age': 21, 'subject': 'html', 'Address': 'ponnur'},
        'ramya':  {'Age': 15, 'subject': 'c/c++', 'Address': 'delhi'}}
  
# create pandas dataframe from dictionary of
# dictionaries
data = pandas.DataFrame(data)
  
# display
data

输出:

方法二:使用 from_dict()

from_dict()用于从字典转换数据帧

句法:

pandas.DataFrame.from_dict(dictionary)

其中,dictionary 是词典的输入词典

示例:从字典字典创建 pandas 数据框。

Python3

# import pandas module
import pandas
  
# create student dictionary of dictionaries with 3 
# students with Age and address
data = {'Ojaswi': {'Age': 15, 'Address': 'Hyderabad'},
        'Rohith':  {'Age': 9, 'Address': 'Hyderabad'},
        'Gnanesh':  {'Age': 15, 'Address': 'Guntur'}}
  
# create pandas dataframe from dictionary of dictionaries 
# using from_dict() method
data = pandas.DataFrame.from_dict(data)
  
# display
data

输出: