python - pandas dataframe无法获取值

标签 python json string pandas series

我有一个具有以下结构的 pandas 数据框:

id, @text, values
0, ....., "{'organizations':['sdfsf','sfdsf','sdfs'],'content':'some text'}"

我正在尝试获取“内容”中的“某些文本”值

Expected output is:

"some text"

我尝试将“{'organizations':['sdfsf','sfdsf','sdfs'],'content':'some text'}”保存在json文档中,但出现“循环引用错误” '

import json
json_data = pandas_dataframe.get(["value"][0])
with open('/pddata.json', 'w') as fp:  
    json.dump(json_data, fp)

最佳答案

使用ast.literal_eval()

示例:

import ast
df = pd.DataFrame([123, 'abc', "{'organizations':['sdfsf','sfdsf','sdfs'],'content':'some text'}"]).T
df.columns = ['id', 'text', 'values']
df
#     id    text    values
# 0  123    abc {'organizations':['sdfsf','sfdsf','sdfs'],'con...

to_fetch = ast.literal_eval(df.iloc[0,2])
type(to_fetch)
# dict

to_fetch['content']
# 'some text'

关于python - pandas dataframe无法获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53636656/

相关文章:

python - python 中文件读取/查找的错误处理

python - 在数据框中查找某些值的索引并将其作为单独的列

javascript - 使用 JSON 和 Serializer 返回多个列表

javascript - 数组/字符串的lastIndexOf结果不一致

string - Lua 模式在行尾时停止

Python OpenCV : capture. 读取返回 true,尽管相机已拔出

python - 有没有办法从所有 30 个数据帧中只提取一列?

c# - 通过 AJAX 从 MVC 返回 JSON

javascript - 将匿名返回值直接分配给对象的属性

javascript - 在破折号或斜杠后将字符串转换为标题大小写