Python 将“对象”类型的字段展开为字段中的单独字段

Python 将“对象”类型的字段展开为字段中的单独字段,python,pandas,Python,Pandas,我正在尝试使用pandas清理CSV格式的数据 我的数据框如下所示: 我想将shot_parameters字段扩展到各个列中 以下是该字段的示例值: 关于如何在pandas中实现这一点,您有什么想法吗?通过ast.literal\u eval创建数据帧列表,传递到DataFrame构造函数并通过以下方式附加到原始数据帧: 编辑: 另一种解决方案是,如果所有值都按,拆分,然后可能按拆分:: L = [dict([y.split(':') for y in x.split(',')])

我正在尝试使用pandas清理CSV格式的数据

我的数据框如下所示:

我想将shot_parameters字段扩展到各个列中

以下是该字段的示例值:


关于如何在pandas中实现这一点,您有什么想法吗?

通过
ast.literal\u eval
创建数据帧列表,传递到
DataFrame
构造函数并通过以下方式附加到原始数据帧:

编辑:

另一种解决方案是,如果所有值都按
拆分,然后可能按
拆分:

L = [dict([y.split(':') for y in x.split(',')])
                        for x in df.pop('shot_paramters').str.strip('{}')]

df1 = pd.DataFrame(L, index=df.index).rename(columns=lambda x: x.strip('"'))
df = df.join(df1)
print (df)
  played_at_time shot_name ampact_angle swing_category impact_offset
0     2019-06-20     VSHOT            0              1           NaN
1     2019-06-21     VSHOT          NaN            NaN            76
编辑1:

df = pd.DataFrame({'played_at_time':['2019-06-20', '2019-06-21'],
                   'shot_paramters':['{"ampact_angle":0:9,"swing_category":1}',
                                     '{"impact_offset"}'],
                    'shot_name':['VSHOT','VSHOT']})
print (df)
  played_at_time                           shot_paramters shot_name
0     2019-06-20  {"ampact_angle":0:9,"swing_category":1}     VSHOT
1     2019-06-21                        {"impact_offset"}     VSHOT

L = [dict([y.split(':', 1) if ':' in y else [y, None] for y in x.split(',')])
                        for x in df.pop('shot_paramters').str.strip('{}')]

df1 = pd.DataFrame(L, index=df.index).rename(columns=lambda x: x.strip('"'))
df = df.join(df1)
print (df)
  played_at_time shot_name ampact_angle swing_category  impact_offset
0     2019-06-20     VSHOT          0:9              1            NaN
1     2019-06-21     VSHOT          NaN            NaN            NaN

通过
ast.literal\u eval
创建数据帧列表,传递到
DataFrame
构造函数并通过以下方式附加到原始数据帧:

编辑:

另一种解决方案是,如果所有值都按
拆分,然后可能按
拆分:

L = [dict([y.split(':') for y in x.split(',')])
                        for x in df.pop('shot_paramters').str.strip('{}')]

df1 = pd.DataFrame(L, index=df.index).rename(columns=lambda x: x.strip('"'))
df = df.join(df1)
print (df)
  played_at_time shot_name ampact_angle swing_category impact_offset
0     2019-06-20     VSHOT            0              1           NaN
1     2019-06-21     VSHOT          NaN            NaN            76
编辑1:

df = pd.DataFrame({'played_at_time':['2019-06-20', '2019-06-21'],
                   'shot_paramters':['{"ampact_angle":0:9,"swing_category":1}',
                                     '{"impact_offset"}'],
                    'shot_name':['VSHOT','VSHOT']})
print (df)
  played_at_time                           shot_paramters shot_name
0     2019-06-20  {"ampact_angle":0:9,"swing_category":1}     VSHOT
1     2019-06-21                        {"impact_offset"}     VSHOT

L = [dict([y.split(':', 1) if ':' in y else [y, None] for y in x.split(',')])
                        for x in df.pop('shot_paramters').str.strip('{}')]

df1 = pd.DataFrame(L, index=df.index).rename(columns=lambda x: x.strip('"'))
df = df.join(df1)
print (df)
  played_at_time shot_name ampact_angle swing_category  impact_offset
0     2019-06-20     VSHOT          0:9              1            NaN
1     2019-06-21     VSHOT          NaN            NaN            NaN

我得到了ValueError:格式错误的节点或字符串:如果将
df1=pd.DataFrame([ast.literal\u eval(x)表示df.pop中的x('shot\u参数')],index=df.index)
更改为
df1=pd.DataFrame([x表示df.pop中的x('shot\u参数')),index=df.index)
它正在工作?@VikasRoy-不幸的是,它意味着数据被破坏了:(有没有办法忽略某些字段并将其设置为null?我得到:ValueError:字典更新序列元素#1的长度为3;需要2我得到的是ValueError:格式错误的节点或字符串:If change
df1=pd.DataFrame([ast.literal\u eval(x)for x in df.pop('shot\u parameters')),index=df.index)
to
df1=pd.DataFrame([x代表df.pop('shot_parameters')中的x,index=df.index)
it working?@VikasRoy-不幸的是,这意味着数据被破坏:(有没有办法忽略某些字段并将其设置为null?我得到:ValueError:dictionary update sequence元素#1的长度为3;2是必需的