python - 将系列字典从数据帧列转换为同一数据帧中的单独列

标签 python pandas dataframe dictionary series

I have this dataframe(df) that looks like this
`
user_id |date       |last_dep_amt| dep_amt| Bin       | Action    
1031    |2017-03-11 |200.0       |100     | 100-200   | [{'A1':[350,400,450],
                                                          'A2':[450,480,490],
                                                          'A3':[500,550,600],
                                                          'A4':[650, 700,850],
                                                          'A5':[750,800,950],
                                                       'Last_5_deposits':[50],
                                                       'num_unique_a1':3,
                                                       'num_unique_a2':4,
                                                       'num_unique_a3':7,
                                                       'num_unique_a4':8,
                                                       'num_unique_a5':9}]
1031    |2017-03-12 |300.0       |120     | 100-200   | [{'A1':[250,300,550],
                                                          'A2':[150,440,460],
                                                          'A3':[250,300,430],
                                                          'A4':[350, 500,650],
                                                          'A5':[650,700,780],
                                                       'Last_5_deposits':[50],
                                                       'num_unique_a1':3,
                                                       'num_unique_a2':4,
                                                       'num_unique_a3':7,
                                                       'num_unique_a4':8,
                                                       'num_unique_a5':9}]
231 |2017-03-14 |350.0       |130     | 100-200   | [{'A1':[250,300,550],
                                                          'A2':[150,440,460],
                                                          'A3':[250,300,430],
                                                          'A4':[350, 500,650],
                                                          'A5':[650,700,780],
                                                       'Last_5_deposits':[50],
                                                       'num_unique_a1':3,
                                                       'num_unique_a2':4,
                                                       'num_unique_a3':7,
                                                       'num_unique_a4':8,
                                                       'num_unique_a5':9}]
`      
Essentially containing 6 columns. Where the last column('Action') of the dataframe is list of dictionary.  

所以我需要将最后一列('Action')分成多列,如下所示 例如: user_id|日期|last_dep_amt| dep_amt|Bin|A1|A2|A3|A4|A5|Last_5_deposits| num_unique_a1|num_unique_a2|num_unique_a3|num_unique_a4|num_unique_a5

关于数据框的了解很少 type(df['Action']) - pandas.core.series.Series 类型(df) - pandas.core.frame.DataFrame

预期输出:“操作”列下的所有子列都必须拆分为单独的列 user_id|日期|last_dep_amt|dep_amt|Bin|A1|A2|A3|A4|A5|Last_5_deposits| num_unique_a1|num_unique_a2|num_unique_a3|num_unique_a4|num_unique_a5 `

  +---------+-----------+--------------+---------+---------+---------------+---------------+---------------+----------------+---------------+-----------------+---------------+---------------+---------------+----------------+----------------+
    | user_id |   date    | last_dep_amt | dep_amt |   Bin   |      A1       |      A2       |      A3       |       A4       |      A5       | Last_5_deposits | num_unique_a1 | num_unique_a2 | num_unique_a3 |  num_unique_a4 | num_unique_a5  |
    +---------+-----------+--------------+---------+---------+---------------+---------------+---------------+----------------+---------------+-----------------+---------------+---------------+---------------+----------------+----------------+
    |    1031 | 3/11/2017 |          200 |     100 | 100-200 | [350,400,450] | [450,480,490] | [500,550,600] | [650, 700,850] | [750,800,950] | [50]            |             3 |             4 |             7 |              8 |              9 |
    +---------+-----------+--------------+---------+---------+---------------+---------------+---------------+----------------+---------------+-----------------+---------------+---------------+---------------+----------------+----------------+

`
Also have attached below the link that contains an image of the expected final output needed from the above dataframe(df)
`
<https://ibb.co/0JyKhHQ>

最佳答案

使用pandas.concat :

df_action=pd.concat([pd.DataFrame(key) for key in df['Action']]).reset_index(drop=True)
new_df=pd.concat([df[['user_id','date','last_dep_amt','dep_amt','Bin']],df_action],axis=1)
print(new_df)
<小时/>

输出:

   user_id        date  last_dep_amt  dep_amt      Bin               A1  \
0     1031  2017-03-11         200.0      100  100-200  [350, 400, 450]   
1     1031  2017-03-12         300.0      120  100-200  [250, 300, 550]   
2      231  2017-03-14         350.0      130  100-200  [250, 300, 550]   

                A2               A3               A4               A5  \
0  [450, 480, 490]  [500, 550, 600]  [650, 700, 850]  [750, 800, 950]   
1  [150, 440, 460]  [250, 300, 430]  [350, 500, 650]  [650, 700, 780]   
2  [150, 440, 460]  [250, 300, 430]  [350, 500, 650]  [650, 700, 780]   

  Last_5_deposits  num_unique_a1  num_unique_a2  num_unique_a3  num_unique_a4  \
0            [50]              3              4              7              8   
1            [50]              3              4              7              8   
2            [50]              3              4              7              8   

   num_unique_a5  
0              9  
1              9  
2              9 

关于python - 将系列字典从数据帧列转换为同一数据帧中的单独列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58191930/

相关文章:

python - 如何将参数传递给 salt 状态文件?

python - 在 Win7 上导入 pyodbc 会导致 DLL 加载失败,错误代码为 193

python - 当两列中有重复的单元格时,如何更改一列中单元格的值

pandas:merge/concat.join 两个 dfs

python - 删除包含特定日期之间数据的行

python - 添加到现有电子表格?

python - 前一行的 Pandas 数据框计算

python - 计算 DataFrame 中每一列的第一个非缺失值

python - 格式化日期时间以使用 pandas 绘制时间序列

python - 敌人不停留在原地