簡體   English   中英

從字典創建 Python DataFrame,其中鍵是列名,值是行

[英]Create Python DataFrame from dictionary where keys are the column names and values form the row

我熟悉 python 但對 panda DataFrames 不熟悉。 我有一本這樣的字典:

a={'b':100,'c':300}

我想將它轉換為 DataFrame,其中 b 和 c 是列名,第一行是 100,300(100 在 b 下面,300 在 c 下面)。 我想要一個可以推廣到更長的字典,包含更多項目的解決方案。 謝謝!

將值作為列表傳遞:

a={'b':[100,],'c':[300,]}
pd.DataFrame(a)

     b    c
0  100  300

或者,如果由於某種原因您不想使用列表,請包含索引:

a={'b':100,'c':300}
pd.DataFrame(a, index=['i',])

     b    c
i  100  300

使用列表作為字典中的值。

import pandas as pd
a = {'b':[100,200],'c':[300,400]}
b = pd.DataFrame(a)

In [4]: b
Out[4]: 
     b    c
0  100  300
1  200  400
loan_sanction_details = pd.DataFrame(event,index=['i',])

其中loan_sanction_details是 dataframe, event是字典。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM