【问题标题】:DataFrame constructor not properly called! with JSON未正确调用 DataFrame 构造函数!使用 JSON
【发布时间】:2019-02-11 04:09:28
【问题描述】:

我正在尝试读取 json 文件并隐藏到数据帧。但我无法使用两种不同的方法来完成。

import os
import pandas as pd


for m_file in os.listdir('.'):
    if m_file.startswith('180830') and m_file.endswith('_out.json'):
        input_file2 = open(m_file, 'r')
        output_json = input_file2.read()

        #output_df = pd.read_json(output_json, orient=str) #gives me ValueError: Expected object or value error
        output_df = pd.DataFrame(eval(output_json)) #gives me the contructor error
        print(output_df)

【问题讨论】:

  • 您的缩进看起来不适合初学者。
  • 已修复,但这不是原因。
  • 你到底为什么用eval()而不是json.load()

标签: python json python-3.x pandas dataframe


【解决方案1】:

熊猫可以读取文件路径。

试试:

import os
import pandas as pd


for m_file in os.listdir('.'):
    if m_file.startswith('180830') and m_file.endswith('_out.json'):
        output_df = pd.DataFrame(m_file) 
        print(output_df)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-27
    • 2014-10-25
    • 2021-06-04
    • 2021-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多