python字典转为数据框_python-将列表从大量字典转换为数据框的问题

这篇博客探讨了如何将包含多个字典的结构转换为DataFrame,特别是处理具有相同前缀键的情况。作者首先创建了一个按键分组的字典,然后尝试将后续字典的值合并到全局字典中。遇到的问题是值可能是DataFrame而非列表,导致类型错误。解决方案是使用`collections.defaultdict`来保持列表类型,并通过`pd.concat`将数据合并成DataFrame。
摘要由CSDN通过智能技术生成

我以这种方式创建了一个字典:

数据如下所示:

GDS3:

ABC_1 ABC_2 BBB_1

cat elf 123

dog run 456

bird burp 789

GDS4:

ABC_3 ABC_4 BCB_a

beer yes 234

wine no 543

gin yes 743

GDS5:

ABC_5 ABC_6 BCD_c

lol yea 543

lmao NaN 446

asl NaN 777

#create a dictionary in which all columns that start with the same 3 characters will be grouped in the same key.

dict_2013 = {k: g for k, g in GDS3.groupby(by=lambda x: x[:3].lower(), axis=1)}

dict_2014 = {k: g for k, g in GDS4.groupby(by=lambda x: x[:3].lower(), axis=1)}

dict_2015 = {k: g for k, g in GDS5.groupby(by=lambda x: x[:3].lower(), axis=1)}

#start with year 2013:

global_dict=dict_2013

#if key in the new dictionary is in the old dictionary then

#add the values from the new d

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值