[英]separate dict from list in pandas dataframe column into different dataframe columns
[{
"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"
}]
my try:我的尝试:
specs: Dataframe containing args column the file sample is attached below规格:Dataframe 包含 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)
I hope that the ast will help you in this case.Here is solution我希望 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
........
is it your desired result?这是你想要的结果吗?
if you desire the same output like in your code, here is the example如果您想要与您的代码中相同的 output,这里是示例
lst1 = [literal_eval(s) for s in df[0]]
pd.DataFrame(lst1)
Just use the dataframe constructor只需使用 dataframe 构造函数
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))
out:出去:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.