Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将json dict转换为df中的行_Python_Json_Pandas_Dictionary - Fatal编程技术网

Python 将json dict转换为df中的行

Python 将json dict转换为df中的行,python,json,pandas,dictionary,Python,Json,Pandas,Dictionary,我已经从url中提取了JSON数据。结果是一本字典。如何转换此字典,使每个键都是一列,时间戳是每行的索引-其中每次调用url时,dict值都对应于行条目 以下是数据: 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

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

以下是数据:

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:必须使用某种集合调用索引(…),传递了“timestamp”


块_数据
包装在列表中

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索引后,如何将其转换为datetime?