【问题标题】:separate dict from list in pandas dataframe column into different dataframe columns将 pandas 数据框列中的列表中的 dict 分离到不同的数据框列中
【发布时间】:2019-11-09 07:36:04
【问题描述】:
[{
"name":"game_time",
"type":"int",
"info":"millisecond count since start of game"
},
{
"name":"round",
"type":"int",
"info":"number of the current round when the even takes place or 0 if no round"
}]

我的尝试:

specs:包含 args 列的数据框,文件示例附在下面

specs['args'].apply(lambda x : x.split('},{')).to_frame()['args'].apply(pd.Series).apply(lambda x : x.str[2:])
specs['args'].apply(pd.Series)

sample file

【问题讨论】:

    标签: python pandas list dataframe dictionary


    【解决方案1】:

    我希望ast在这种情况下对您有所帮助。这是解决方案

    结果的一个版本

    import pandas as pd
    from ast import literal_eval
    
    df = pd.read_csv('test_.csv', header = None)
    df
    
    Out[1]:
    
               0
        0   [{"name":"game_time","type":"int","info":"mill...
        1   [{"name":"game_time","type":"int","info":"mill...
        2   [{"name":"game_time","type":"int","info":"mill...
        3   [{"name":"game_time","type":"int","info":"mill..
    
    lst = [m for s in df[0] for m in literal_eval(s)]
    lst
    
    Out[2]:
    
    [{'name': 'game_time',
      'type': 'int',
      'info': 'millisecond count since start of game'},
     {'name': 'round',
      'type': 'int',
      'info': 'number of the current round when the event takes place or 0  if no round'},
     {'name': 'level',
      'type': 'int',
      'info': 'number of the current level when the event takes place or 0     if no level'},
     {'name': 'description',.......
    
    
    pd.DataFrame.from_dict(lst)
    
    Out[3]:
    
                                                         info   name        type
        0   millisecond count since start of game               game_time   int
        1   number of the current round when the event tak...   round       int
        2   number of the current level when the event tak...   level       int
        3   the text or description of the instruction          description string
        ........
    

    这是你想要的结果吗?

    另一个版本的结果

    如果您想要与代码中相同的输出,这里是示例

    lst1 = [literal_eval(s) for s in df[0]]
    pd.DataFrame(lst1)
    

    【讨论】:

      【解决方案2】:

      只需使用数据框构造函数

      data = [{
      "name":"game_time",
      "type":"int",
      "info":"millisecond count since start of game"
      },
      {
      "name":"round",
      "type":"int",
      "info":"number of the current round when the even takes place or 0 if no round"
      }]
      
      print(pd.DataFrame(data))
      

      出来:

                                                      info       name type
      0              millisecond count since start of game  game_time  int
      1  number of the current round when the even take...      round  int
      

      【讨论】:

        猜你喜欢
        • 2019-06-18
        • 2017-08-29
        • 2017-05-02
        • 2017-05-02
        • 2021-08-21
        • 1970-01-01
        • 1970-01-01
        • 2021-11-18
        • 2015-01-19
        相关资源
        最近更新 更多