简体   繁体   English

使用多维数组从字典创建 pd.DataFrame

[英]Create pd.DataFrame from dictionary with multi-dimensional array

I've the following dictionary:我有以下字典:

dictA = {'A': [[1, 2, 3], [1, 2, 3], [1, 2, 3]],
         'B': [[4, 4, 4], [4, 4, 4],],
         'C': [[4, 6, 0]]
        }

I want to convert it to a pd.DataFrame() , expecting this:我想将它转换为pd.DataFrame() ,期待这个:

id       ColA        ColB        ColC
0         1           4           4
1         2           4           6
2         3           4           0
3         1           4           
4         2           4
5         3           4
6         1
7         2
8         3

How can I do that?我怎样才能做到这一点? I'm trying我想

pd.DataFrame(dictAll.items(), columns=['ColA', 'ColB', 'ColC'])

But it obviously doesn't work!但这显然行不通!

Here is how:方法如下:

import pandas as pd
import numpy as np

dictA = {'A': [[1, 2, 3], [1, 2, 3], [1, 2, 3]],
         'B': [[4, 4, 4], [4, 4, 4],],
         'C': [[4, 6, 0]]}

df = pd.DataFrame(dict([(f'Col{k}', pd.Series([a for b in v for a in b])) for k,v in dictA.items()])).replace(np.nan, '')
print(df)

Output:输出:

   ColA ColB ColC
0     1    4    4
1     2    4    6
2     3    4    0
3     1    4     
4     2    4     
5     3    4     
6     1          
7     2          
8     3  

Now, let's have a look at the problem one step at a time.现在,让我们一步一步地看一下这个问题。

  1. The first thing we might try is simply:我们可以尝试的第一件事很简单:

     df = pd.DataFrame(dictA) print(df)

    Which, of course, return this error:当然,这会返回此错误:

     ValueError: arrays must all be same length
  2. So now we need a way to be able to create dataframes from a dict with arrays of different lengths.所以现在我们需要一种能够从具有不同长度数组的dict创建数据帧的方法。 For that, we can:为此,我们可以:

     df = pd.DataFrame(dict([(k, pd.Series(v)) for k, v in dictA.items()])) print(df)

    Output:输出:

     ABC 0 [1, 2, 3] [4, 4, 4] [4, 6, 0] 1 [1, 2, 3] [4, 4, 4] NaN 2 [1, 2, 3] NaN NaN
  3. We want the dataframe to be vertical, so for each iteration, flatten out the lists with a list comprehension:我们希望数据框是垂直的,因此对于每次迭代,使用列表理解将列表展平:

     df = pd.DataFrame(dict([(k, pd.Series([a for b in v for a in b])) for k, v in dictA.items()])) print(df)

    Output:输出:

     ABC 0 1 4.0 4.0 1 2 4.0 6.0 2 3 4.0 0.0 3 1 4.0 NaN 4 2 4.0 NaN 5 3 4.0 NaN 6 1 NaN NaN 7 2 NaN NaN 8 3 NaN NaN
  4. Now we want to replace all the NaN s with blanks.现在我们想用空格替换所有的NaN For that, we need to import numpy as np , and do:为此,我们需要import numpy as np ,然后执行:

     df = pd.DataFrame(dict([(k, pd.Series([a for b in v for a in b])) for k, v in dictA.items()])).replace(np.nan, '') print(df)

    Output:输出:

     ABC 0 1 4 4 1 2 4 6 2 3 4 0 3 1 4 4 2 4 5 3 4 6 1 7 2 8 3
  5. Finally use formatted string to convert the letters into "Col" letters:最后使用格式化字符串将字母转换为"Col"字母:

     df = pd.DataFrame(dict([(f'Col{k}', pd.Series([a for b in v for a in b])) for k,v in dictA.items()])).replace(np.nan, '') print(df)

    Output:输出:

     ColA ColB ColC 0 1 4 4 1 2 4 6 2 3 4 0 3 1 4 4 2 4 5 3 4 6 1 7 2 8 3

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM