简体   繁体   English

从形式{index:行值列表}中的字典构造Pandas DataFrame

[英]Construct Pandas DataFrame from dictionary in form {index: list of row values}

I've managed to do this using: 我设法使用以下方法:

dft = pd.DataFrame.from_dict({
                    0: [50, 45, 00, 00], 
                    1: [53, 48, 00, 00],
                    2: [56, 53, 00, 00],
                    3: [54, 49, 00, 00],
                    4: [53, 48, 00, 00],
                    5: [50, 45, 00, 00]
                    }, orient='index'
                    )

Done like this, the constructor looks just like the DataFrame making it easy to read/edit: 完成后,构造函数看起来就像DataFrame一样,易于阅读/编辑:

>>> dft
    0   1   2   3
0   50  45  0   0
1   53  48  0   0
2   56  53  0   0
3   54  49  0   0
4   53  48  0   0
5   50  45  0   0

But the DataFrame.from_dict constructor doesn't have a columns parameter, so giving the columns sensible names takes an additional step: DataFrame.from_dict构造函数没有columns参数,因此为列提供合理的名称需要额外的步骤:

dft.columns = ['A', 'B', 'C', 'D']

This seems clunky for such a handy (eg for unit tests) way to initialise DataFrames. 对于这种方便(例如用于单元测试)初始化DataFrames的方式来说,这似乎很笨拙。

So I wonder: is there a better way? 所以我想知道:有更好的方法吗?

Alternatively you could use DataFrame.from_items() to construct the DataFrame from your dictionary; 或者,您可以使用DataFrame.from_items()从您的字典构造DataFrame; this allows you to pass in the column names at the same time. 这允许您同时传入列名。

For example, if d is your dictionary: 例如,如果d是你的字典:

d = {0: [50, 45, 0, 0],
     1: [53, 48, 0, 0],
     2: [56, 53, 0, 0],
     3: [54, 49, 0, 0],
     4: [53, 48, 0, 0],
     5: [50, 45, 0, 0]}

The data is d.items() and the orient is again 'index' . 数据是d.items() ,而orient也是'index' The dictionary keys become the index values: 字典键成为索引值:

>>> pd.DataFrame.from_items(d.items(), 
                            orient='index', 
                            columns=['A','B','C','D'])
    A   B  C  D
0  50  45  0  0
1  53  48  0  0
2  56  53  0  0
3  54  49  0  0
4  53  48  0  0
5  50  45  0  0

In Python 2 you can use d.iteritems() to yield the contents of the dictionary to avoid creating another list in memory. 在Python 2中,您可以使用d.iteritems()来生成字典的内容,以避免在内存中创建另一个列表。

One way to do that is the following: 一种方法是:

df = pd.DataFrame.from_dict({
0: {"A":50, "B":40},
1: {"A":51, "B":30}}, orient='index')

However, for quick test initialization I would probably prefer your way + then setting the columns. 但是,为了快速测试初始化​​,我可能更喜欢你的方式+然后设置列。

You could try: 你可以尝试:

x=pd.DataFrame({0:[50,45],1:[53,48],2:[56,53]}, index=["A","B"]).transpose()

But it's still odd as you are specifying the standard index as keys for your dictionary. 但是,当您将标准索引指定为字典的键时,它仍然很奇怪。

Why not directly 为何不直接

x = pd.DataFrame({"A":[50,53,56],"B":...})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用键作为列和行索引从字典构造pandas DataFrame - How to construct pandas DataFrame from dictionary with key as column and row index 从具有值列表的 3 级嵌套字典中的项目构造 pandas DataFrame - Construct pandas DataFrame from items in 3 level nested dictionary with to list of values 从(row,col,values)元组列表构造pandas DataFrame - Construct pandas DataFrame from list of tuples of (row,col,values) 从嵌套字典构造 pandas 多索引 dataframe - construct pandas multi index dataframe from nested dictionary 如何从包含数组中嵌套值列表、字典中的输出构建熊猫数据框 - How to construct a pandas dataframe from an output consisting of a nested list of values within an array, within a dictionary 来自字典中嵌套值的Pandas Dataframe索引 - Pandas Dataframe index from nested values in dictionary 从嵌套字典中的项目构造一个 pandas DataFrame,列表作为内部值 - Construct a pandas DataFrame from items in a nested dictionary with lists as inner values 用字典中的值替换 pandas dataframe 中的一行 - Replace a row in a pandas dataframe with values from dictionary Python pandas:从 dataframe 的每一行构造列表数据类对象 - Python pandas: construct list dataclass objects from each row of a dataframe 从字典列表中提取值 pandas dataframe - Extracting values from dictionary list in pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM