将Pandas dataframe列中的列表中的dict分为不同的dataframe列

[{
"name":"game_time","type":"int","info":"millisecond count since start of game"
},{
"name":"round","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

ilovec042 回答:将Pandas dataframe列中的列表中的dict分为不同的dataframe列

在这种情况下,我希望ast能为您提供帮助。

结果的一个版本

0000:00:0:00:00

这是您想要的结果吗?

结果的另一个版本

如果您希望获得与代码中相同的输出,请参见以下示例

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","info":"mill...
    2   [{"name":"game_time","info":"mill...
    3   [{"name":"game_time","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','info': 'number of the current round when the event takes place or 0  if no round'},{'name': 'level','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
    ........
,

只需使用数据框构造函数

    RotationCorrectionRight(FPT);
    RotationCorrectionRight(BPT);

    RotationCorrectionLeft(FDT);
    RotationCorrectionLeft(BDT);

退出:

data = [{
"name":"game_time","info":"millisecond count since start of game"
},{
"name":"round","info":"number of the current round when the even takes place or 0 if no round"
}]

print(pd.DataFrame(data))
本文链接:https://www.f2er.com/3133660.html

大家都在问