簡體   English   中英

將Pandas DataFrame轉換為Python列表

[英]Convert Pandas DataFrame to Python list

我有以下數據幀:

In [137]: counts
Out[137]: 
SourceColumnID                    3029903181  3029903182  3029903183  3029903184  ResponseCount
ColID      QuestionID RowID                                                                    
3029903193 316923119  3029903189         773         788         778         803           3142
3029903194 316923119  3029903189         766         799         782         773           3120

[2 rows x 5 columns]

當我通過iloc訪問它時,它適用於我想要的東西:

In [138]: counts.iloc[0][3029903181]
Out[138]: 773

但是當我將它轉換為dict時,它將以不再以相同方式訪問的方式對其進行格式化:

In [139]: counts.to_dict()
Out[139]: 
{3029903181: {(3029903193, 316923119, 3029903189): 773,
  (3029903194, 316923119, 3029903189): 766},
 3029903182: {(3029903193, 316923119, 3029903189): 788,
  (3029903194, 316923119, 3029903189): 799},
 3029903183: {(3029903193, 316923119, 3029903189): 778,
  (3029903194, 316923119, 3029903189): 782},
 3029903184: {(3029903193, 316923119, 3029903189): 803,
  (3029903194, 316923119, 3029903189): 773},
 'ResponseCount': {(3029903193, 316923119, 3029903189): 3142,
  (3029903194, 316923119, 3029903189): 3120}}

In [140]: counts.to_dict('list')
Out[140]: 
{3029903181: [773, 766],
 3029903182: [788, 799],
 3029903183: [778, 782],
 3029903184: [803, 773],
 'ResponseCount': [3142, 3120]}

我需要將此數據結構轉換為標准python對象,以返回API以使用它。

我應該以不同的格式創建表嗎?

我從這個DataFrame開始:

In [141]: df
Out[141]: 
        ColID  QuestionID  ResponseCount       RowID  SourceColumnID
0  3029903193   316923119            773  3029903189      3029903181
1  3029903193   316923119            788  3029903189      3029903182
2  3029903193   316923119            778  3029903189      3029903183
3  3029903193   316923119            803  3029903189      3029903184
4  3029903194   316923119            766  3029903189      3029903181
5  3029903194   316923119            799  3029903189      3029903182
6  3029903194   316923119            782  3029903189      3029903183
7  3029903194   316923119            773  3029903189      3029903184

[8 rows x 5 columns]

並將其轉換為如下所示的數據透視表:

counts = df.pivot_table(values='ResponseCount', rows=['ColID', 'QuestionID', 'RowID'], cols='SourceColumnID', aggfunc='sum')

我真的在尋找看起來像這樣的數據結構:

[
  {
    'QuestionID': 316923119, 
    'RowID': 3029903189, 
    'ColID': 3029903193, 
    '3029903181': 773,
    '3029903182': 788,
    '3029903183': 778,
    '3029903184': 803,
    'ResponseCount': 3142
  },
  {
    'QuestionID': 316923119, 
    'RowID': 3029903189, 
    'ColID': 3029903194, 
    '3029903181': 766,
    '3029903182': 799,
    '3029903183': 782,
    '3029903184': 773,
    'ResponseCount': 3120
  },
]

我相信你想要counts.reset_index().to_dict('records')

to_dict使用'records'可以得到一個to_dict列表,每行一個dict,這就是你想要的。 您需要使用reset_index()以列的形式獲取索引信息(因為'records'會拋棄索引)。 從概念上講,你說你想要的詞匯不區分數據透視表索引中的內容和列中的內容(你只想將所有索引列標簽作為字典中的鍵),所以你需要reset_index來刪除索引/列的區別。

暫無
暫無

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

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