我有一个来自CoinMarketCap API的json输出,我想知道如何将它转换成DataFrame?{'1027':{‘1027’:{‘循环_供应’:119359483.749,'cmc_rank':2,'date_added':'2015-08-07T00:00:00.000Z','id':1027,'is_active':1,'is_fiat':0,‘上次更新’:'2022-01-29T12:48:00.000Z','max_supply':无,“名称”:“Ethereum”,“num_market_pairs”:5482,“platform”:None,“quote”:{'USD':{‘全倍稀释_市场_上限’:307402654581.68,‘最后一次更新’:'2022-01-29T12:48:00.000Z',‘市场_上限’:307402654581.679,‘市值_主导地位’:17.8235,‘百分之变化_1h’:1.06045615,‘百分变化_24’:8.70029864,“百分_变化_30d”:-31.11373609,‘百分_变化_60D’:-44.18789242,‘百分_变化_7d’:4.3122346,‘百分比_变化_90d’:-38.78235831,‘价格’:2575.435524068731,‘量_24小时’:12753359871.597496,‘量_变化_24小时’:-18.1848}},‘自我报告_循环_供应’:无,‘自我_报告_市场_上限’:‘无,’水泡‘:’弹琴‘,‘符号’:'ETH',‘标签’:‘可挖掘’,'pow',‘智能-合同’,‘ethereum生态系统’,‘二进制-智能-链’,'coinbase-ventures-portfolio',‘三箭-资本-投资组合’,‘多链-资本-投资组合’,‘宾利-实验室-投资组合’,‘区块链-资本-投资组合’,‘boostvc投资组合’,‘cms-持有-投资组合’,‘G-投资组合,’蜻蜓-资本-投资组合‘,“电动-资本-投资组合”、“结构-风险-投资组合”、“框架-风险-投资组合”、“哈什基-资本-投资组合”、“kenetic-capital-portfolio”、“huobi-capital-portfolio”、“alameda-research-portfolio”、“a16z-portfolio”、“1确认-投资组合”、“winklevoss-capital-portfolio”、“usv-portfolio”、“pantera ventures-portfolio”、“pantera-capital-portfolio”、“Multi铜币-资本-投资组合”、“范例-投资组合”,“总量_供应”:119359483.749},“状态”:{‘信用_计数’:1,‘运行’:47,'error_code':0,'error_message':None,‘None’,‘time戳’:‘2022-01-29T12:49:23.283 Z’}}
发布于 2022-01-29 13:26:16
你可以这样做:
这是你的数据:
data = {'data': {'1027': {'circulating_supply': 119359483.749, 'cmc_rank': 2, 'date_added': '2015-08-07T00:00:00.000Z', 'id': 1027, 'is_active': 1, 'is_fiat': 0, 'last_updated': '2022-01-29T12:48:00.000Z', 'max_supply': None, 'name': 'Ethereum', 'num_market_pairs': 5482, 'platform': None, 'quote': {'USD': {'fully_diluted_market_cap': 307402654581.68, 'last_updated': '2022-01-29T12:48:00.000Z', 'market_cap': 307402654581.679, 'market_cap_dominance': 17.8235, 'percent_change_1h': 1.06045615, 'percent_change_24h': 8.70029864, 'percent_change_30d': -31.11373609, 'percent_change_60d': -44.18789242, 'percent_change_7d': 4.3122346, 'percent_change_90d': -38.78235831, 'price': 2575.435524068731, 'volume_24h': 12753359871.597496, 'volume_change_24h': -18.1848}}, 'self_reported_circulating_supply': None, 'self_reported_market_cap': None, 'slug': 'ethereum', 'symbol': 'ETH', 'tags': ['mineable', 'pow', 'smart-contracts', 'ethereum-ecosystem', 'binance-smart-chain', 'coinbase-ventures-portfolio', 'three-arrows-capital-portfolio', 'polychain-capital-portfolio', 'binance-labs-portfolio', 'blockchain-capital-portfolio', 'boostvc-portfolio', 'cms-holdings-portfolio', 'dcg-portfolio', 'dragonfly-capital-portfolio', 'electric-capital-portfolio', 'fabric-ventures-portfolio', 'framework-ventures-portfolio', 'hashkey-capital-portfolio', 'kenetic-capital-portfolio', 'huobi-capital-portfolio', 'alameda-research-portfolio', 'a16z-portfolio', '1confirmation-portfolio', 'winklevoss-capital-portfolio', 'usv-portfolio', 'placeholder-ventures-portfolio', 'pantera-capital-portfolio', 'multicoin-capital-portfolio', 'paradigm-portfolio'], 'total_supply': 119359483.749}}, 'status': {'credit_count': 1, 'elapsed': 47, 'error_code': 0, 'error_message': None, 'notice': None, 'timestamp': '2022-01-29T12:49:23.283Z'}}定义取消嵌套任何json的下列函数
def flatten_nested_json_df(df):
df = df.reset_index()
s = (df.applymap(type) == list).all()
list_columns = s[s].index.tolist()
s = (df.applymap(type) == dict).all()
dict_columns = s[s].index.tolist()
while len(list_columns) > 0 or len(dict_columns) > 0:
new_columns = []
for col in dict_columns:
horiz_exploded = pd.json_normalize(df[col]).add_prefix(f'{col}.')
horiz_exploded.index = df.index
df = pd.concat([df, horiz_exploded], axis=1).drop(columns=[col])
new_columns.extend(horiz_exploded.columns) # inplace
for col in list_columns:
#print(f"exploding: {col}")
df = df.drop(columns=[col]).join(df[col].explode().to_frame())
new_columns.append(col)
s = (df[new_columns].applymap(type) == list).all()
list_columns = s[s].index.tolist()
s = (df[new_columns].applymap(type) == dict).all()
dict_columns = s[s].index.tolist()
return df并采取以下行动:
results = pd.json_normalize(data)
df = pd.DataFrame(results)
outdf = flatten_nested_json_df(df)这意味着:
index data.1027.circulating_supply data.1027.cmc_rank \
0 0 1.193595e+08 2
0 0 1.193595e+08 2
0 0 1.193595e+08 2
0 0 1.193595e+08 2
0 0 1.193595e+08 2
0 0 1.193595e+08 2
0 0 1.193595e+08 2
0 0 1.193595e+08 2
0 0 1.193595e+08 2
0 0 1.193595e+08 2
0 0 1.193595e+08 2
0 0 1.193595e+08 2
0 0 1.193595e+08 2
0 0 1.193595e+08 2
0 0 1.193595e+08 2
0 0 1.193595e+08 2
0 0 1.193595e+08 2
0 0 1.193595e+08 2
0 0 1.193595e+08 2
0 0 1.193595e+08 2
0 0 1.193595e+08 2
0 0 1.193595e+08 2
0 0 1.193595e+08 2
0 0 1.193595e+08 2
0 0 1.193595e+08 2
0 0 1.193595e+08 2
0 0 1.193595e+08 2
0 0 1.193595e+08 2
0 0 1.193595e+08 2
data.1027.date_added data.1027.id data.1027.is_active \
0 2015-08-07T00:00:00.000Z 1027 1
0 2015-08-07T00:00:00.000Z 1027 1
0 2015-08-07T00:00:00.000Z 1027 1
0 2015-08-07T00:00:00.000Z 1027 1
0 2015-08-07T00:00:00.000Z 1027 1
0 2015-08-07T00:00:00.000Z 1027 1
0 2015-08-07T00:00:00.000Z 1027 1
0 2015-08-07T00:00:00.000Z 1027 1
0 2015-08-07T00:00:00.000Z 1027 1
0 2015-08-07T00:00:00.000Z 1027 1
0 2015-08-07T00:00:00.000Z 1027 1
0 2015-08-07T00:00:00.000Z 1027 1
0 2015-08-07T00:00:00.000Z 1027 1
0 2015-08-07T00:00:00.000Z 1027 1
0 2015-08-07T00:00:00.000Z 1027 1
0 2015-08-07T00:00:00.000Z 1027 1
0 2015-08-07T00:00:00.000Z 1027 1
0 2015-08-07T00:00:00.000Z 1027 1
0 2015-08-07T00:00:00.000Z 1027 1
0 2015-08-07T00:00:00.000Z 1027 1
0 2015-08-07T00:00:00.000Z 1027 1
0 2015-08-07T00:00:00.000Z 1027 1
0 2015-08-07T00:00:00.000Z 1027 1
0 2015-08-07T00:00:00.000Z 1027 1
0 2015-08-07T00:00:00.000Z 1027 1
0 2015-08-07T00:00:00.000Z 1027 1
0 2015-08-07T00:00:00.000Z 1027 1
0 2015-08-07T00:00:00.000Z 1027 1
0 2015-08-07T00:00:00.000Z 1027 1
data.1027.is_fiat data.1027.last_updated data.1027.max_supply \
0 0 2022-01-29T12:48:00.000Z None
0 0 2022-01-29T12:48:00.000Z None
0 0 2022-01-29T12:48:00.000Z None
0 0 2022-01-29T12:48:00.000Z None
0 0 2022-01-29T12:48:00.000Z None
0 0 2022-01-29T12:48:00.000Z None
0 0 2022-01-29T12:48:00.000Z None
0 0 2022-01-29T12:48:00.000Z None
0 0 2022-01-29T12:48:00.000Z None
0 0 2022-01-29T12:48:00.000Z None
0 0 2022-01-29T12:48:00.000Z None
0 0 2022-01-29T12:48:00.000Z None
0 0 2022-01-29T12:48:00.000Z None
0 0 2022-01-29T12:48:00.000Z None
0 0 2022-01-29T12:48:00.000Z None
0 0 2022-01-29T12:48:00.000Z None
0 0 2022-01-29T12:48:00.000Z None
0 0 2022-01-29T12:48:00.000Z None
0 0 2022-01-29T12:48:00.000Z None
0 0 2022-01-29T12:48:00.000Z None
0 0 2022-01-29T12:48:00.000Z None
0 0 2022-01-29T12:48:00.000Z None
0 0 2022-01-29T12:48:00.000Z None
0 0 2022-01-29T12:48:00.000Z None
0 0 2022-01-29T12:48:00.000Z None
0 0 2022-01-29T12:48:00.000Z None
0 0 2022-01-29T12:48:00.000Z None
0 0 2022-01-29T12:48:00.000Z None
0 0 2022-01-29T12:48:00.000Z None
data.1027.name ... data.1027.slug data.1027.symbol \
0 Ethereum ... ethereum ETH
0 Ethereum ... ethereum ETH
0 Ethereum ... ethereum ETH
0 Ethereum ... ethereum ETH
0 Ethereum ... ethereum ETH
0 Ethereum ... ethereum ETH
0 Ethereum ... ethereum ETH
0 Ethereum ... ethereum ETH
0 Ethereum ... ethereum ETH
0 Ethereum ... ethereum ETH
0 Ethereum ... ethereum ETH
0 Ethereum ... ethereum ETH
0 Ethereum ... ethereum ETH
0 Ethereum ... ethereum ETH
0 Ethereum ... ethereum ETH
0 Ethereum ... ethereum ETH
0 Ethereum ... ethereum ETH
0 Ethereum ... ethereum ETH
0 Ethereum ... ethereum ETH
0 Ethereum ... ethereum ETH
0 Ethereum ... ethereum ETH
0 Ethereum ... ethereum ETH
0 Ethereum ... ethereum ETH
0 Ethereum ... ethereum ETH
0 Ethereum ... ethereum ETH
0 Ethereum ... ethereum ETH
0 Ethereum ... ethereum ETH
0 Ethereum ... ethereum ETH
0 Ethereum ... ethereum ETH
data.1027.total_supply status.credit_count status.elapsed \
0 1.193595e+08 1 47
0 1.193595e+08 1 47
0 1.193595e+08 1 47
0 1.193595e+08 1 47
0 1.193595e+08 1 47
0 1.193595e+08 1 47
0 1.193595e+08 1 47
0 1.193595e+08 1 47
0 1.193595e+08 1 47
0 1.193595e+08 1 47
0 1.193595e+08 1 47
0 1.193595e+08 1 47
0 1.193595e+08 1 47
0 1.193595e+08 1 47
0 1.193595e+08 1 47
0 1.193595e+08 1 47
0 1.193595e+08 1 47
0 1.193595e+08 1 47
0 1.193595e+08 1 47
0 1.193595e+08 1 47
0 1.193595e+08 1 47
0 1.193595e+08 1 47
0 1.193595e+08 1 47
0 1.193595e+08 1 47
0 1.193595e+08 1 47
0 1.193595e+08 1 47
0 1.193595e+08 1 47
0 1.193595e+08 1 47
0 1.193595e+08 1 47
status.error_code status.error_message status.notice \
0 0 None None
0 0 None None
0 0 None None
0 0 None None
0 0 None None
0 0 None None
0 0 None None
0 0 None None
0 0 None None
0 0 None None
0 0 None None
0 0 None None
0 0 None None
0 0 None None
0 0 None None
0 0 None None
0 0 None None
0 0 None None
0 0 None None
0 0 None None
0 0 None None
0 0 None None
0 0 None None
0 0 None None
0 0 None None
0 0 None None
0 0 None None
0 0 None None
0 0 None None
status.timestamp data.1027.tags
0 2022-01-29T12:49:23.283Z mineable
0 2022-01-29T12:49:23.283Z pow
0 2022-01-29T12:49:23.283Z smart-contracts
0 2022-01-29T12:49:23.283Z ethereum-ecosystem
0 2022-01-29T12:49:23.283Z binance-smart-chain
0 2022-01-29T12:49:23.283Z coinbase-ventures-portfolio
0 2022-01-29T12:49:23.283Z three-arrows-capital-portfolio
0 2022-01-29T12:49:23.283Z polychain-capital-portfolio
0 2022-01-29T12:49:23.283Z binance-labs-portfolio
0 2022-01-29T12:49:23.283Z blockchain-capital-portfolio
0 2022-01-29T12:49:23.283Z boostvc-portfolio
0 2022-01-29T12:49:23.283Z cms-holdings-portfolio
0 2022-01-29T12:49:23.283Z dcg-portfolio
0 2022-01-29T12:49:23.283Z dragonfly-capital-portfolio
0 2022-01-29T12:49:23.283Z electric-capital-portfolio
0 2022-01-29T12:49:23.283Z fabric-ventures-portfolio
0 2022-01-29T12:49:23.283Z framework-ventures-portfolio
0 2022-01-29T12:49:23.283Z hashkey-capital-portfolio
0 2022-01-29T12:49:23.283Z kenetic-capital-portfolio
0 2022-01-29T12:49:23.283Z huobi-capital-portfolio
0 2022-01-29T12:49:23.283Z alameda-research-portfolio
0 2022-01-29T12:49:23.283Z a16z-portfolio
0 2022-01-29T12:49:23.283Z 1confirmation-portfolio
0 2022-01-29T12:49:23.283Z winklevoss-capital-portfolio
0 2022-01-29T12:49:23.283Z usv-portfolio
0 2022-01-29T12:49:23.283Z placeholder-ventures-portfolio
0 2022-01-29T12:49:23.283Z pantera-capital-portfolio
0 2022-01-29T12:49:23.283Z multicoin-capital-portfolio
0 2022-01-29T12:49:23.283Z paradigm-portfolio
[29 rows x 37 columns]https://stackoverflow.com/questions/70905826
复制相似问题