从熊猫数据帧中删除字典引用



下面是我的数据帧的输出:

              0                                  1  
0  {"time": "2016-03-28T23:23:12Z"      "target": "Raffi-Antilian"}  
1  {"time": "2016-03-28T23:23:12Z"      "target": "Caroline-Kaiser"}

如何将单个记录从类型字典转换为普通数据帧记录,其中列名是字典键,记录值是字典值?我想要的输出应该是:

              Time                Target  
0  2016-03-28T23:23:12Z      Raffi-Antilian   
1  2016-03-28T23:23:12Z      Caroline-Kaiser

我有大约 2000 条记录,感谢任何帮助/指导。

import json
data = []
with open('filename', 'r') as f:
    for line in f:
        data.append(json.loads(line))
pd.DataFrame(data)

Out[49]: 
            target                  time
0   Raffi-Antilian  2016-03-28T23:23:12Z
1  Caroline-Kaiser  2016-03-28T23:23:12Z

如果文件中没有;,您可以使用sep=';' read_csv,因此所有数据都在一个Series中。然后按ast.literal_eval和上次使用pd.DataFramestring转换为dictionary

import pandas as pd
import ast
import io
temp=u"""{"time": "2016-03-28T23:23:12Z","target": "Raffi-Antilian"}  
{"time": "2016-03-28T23:23:12Z","target": "Caroline-Kaiser"}"""
#after testing replace io.StringIO(temp) to filename
s = pd.read_csv(io.StringIO(temp), index_col=None, header=None, sep=';', squeeze=True)
print (s)
0    {"time": "2016-03-28T23:23:12Z","target": "Raf...
1    {"time": "2016-03-28T23:23:12Z","target": "Car...
Name: 0, dtype: object
L = s.apply(lambda x: ast.literal_eval(x)).tolist()
print (L)
[{'time': '2016-03-28T23:23:12Z', 'target': 'Raffi-Antilian'}, 
 {'time': '2016-03-28T23:23:12Z', 'target': 'Caroline-Kaiser'}]
print (pd.DataFrame(L))
            target                  time
0   Raffi-Antilian  2016-03-28T23:23:12Z
1  Caroline-Kaiser  2016-03-28T23:23:12Z

编辑:

另一个单线解决方案:

import pandas as pd
import json
print (pd.DataFrame([json.loads(line.strip()) for line in open('file.txt')]))
            target                  time
0   Raffi-Antilian  2016-03-28T23:23:12Z
1  Caroline-Kaiser  2016-03-28T23:23:12Z

相关内容

  • 没有找到相关文章

最新更新