【问题标题】:Convert json dict to row in pandas df将json dict转换为pandas df中的行
【发布时间】:2018-02-08 15:30:45
【问题描述】:

我从一个 url 中提取了 JSON 数据。结果是一本字典。如何转换这个字典,使每个键都是一列,时间戳是每一行的索引 - 每次调用 url 时,dict values 对应于行条目?

这是数据:

with urllib.request.urlopen('https://api.blockchain.info/stats') as url:
    block_data = json.loads(url.read().decode())

# Convert to Pandas
block_df = pd.DataFrame(block_data)

我试过了:

block_df = pd.DataFrame(block_data)
block_df = pd.DataFrame(block_data, index = 'timestamp')
block_df = pd.DataFrame.from_dict(block_data)
block_df = pd.DataFrame.from_dict(block_data, orient = 'columns')

但所有尝试都会给出不同的错误:

ValueError: 如果使用所有标量值,则必须传递索引

TypeError: Index(...) 必须使用某种集合调用,'timestamp' 已通过

【问题讨论】:

    标签: python json pandas dictionary


    【解决方案1】:

    block_data 包装在一个列表中

    pd.DataFrame([block_data]).set_index('timestamp')
    
                   blocks_size    difficulty  estimated_btc_sent  estimated_transaction_volume_usd     hash_rate  market_price_usd  miners_revenue_btc  miners_revenue_usd  minutes_between_blocks  n_blocks_mined  n_blocks_total   n_btc_mined    n_tx  nextretarget   total_btc_sent  total_fees_btc           totalbc  trade_volume_btc  trade_volume_usd
    timestamp                                                                                                                                                                                                                                                                                                                                                
    1504121943000    167692649  888171856257      24674767461479                      1.130867e+09  7.505715e+09           4583.09                2540         11645247.85                    7.92             170          482689  212500000000  281222        483839  174598204968248     41591624963  1653361250000000          43508.93      1.994054e+08
    

    带有datetime 索引。

    df = pd.DataFrame([block_data]).set_index('timestamp')
    df.index = pd.to_datetime(df.index, unit='ms')
    df
    
                         blocks_size    difficulty  estimated_btc_sent  estimated_transaction_volume_usd     hash_rate  market_price_usd  miners_revenue_btc  miners_revenue_usd  minutes_between_blocks  n_blocks_mined  n_blocks_total   n_btc_mined    n_tx  nextretarget   total_btc_sent  total_fees_btc           totalbc  trade_volume_btc  trade_volume_usd
    timestamp                                                                                                                                                                                                                                                                                                                                                      
    2017-08-30 19:39:03    167692649  888171856257      24674767461479                      1.130867e+09  7.505715e+09           4583.09                2540         11645247.85                    7.92             170          482689  212500000000  281222        483839  174598204968248     41591624963  1653361250000000          43508.93      1.994054e+08
    

    【讨论】:

    • 谢谢,太好了。还有一个问题 - 如何在 unix timstamp 索引设置后将其转换为日期时间?
    猜你喜欢
    • 2021-11-30
    • 2020-10-17
    • 1970-01-01
    • 2017-01-28
    • 2020-02-09
    • 1970-01-01
    • 2018-11-30
    • 2017-05-17
    • 1970-01-01
    相关资源
    最近更新 更多