鱼C论坛

 找回密码
 立即注册
查看: 1069|回复: 1

[已解决]大师看看,我的这个怎么不能形成数据帧呢?求帮忙,谢谢

[复制链接]
发表于 2023-5-12 08:44:43 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
response = '''{"version":"1b6e727c2b13c5a769f82ec8886be915","result":{"pages":2,"data":[{"SECURITY_CODE":"000021","SECUCODE":"000021.SZ","SECURITY_NAME_ABBR":"深科技","TRADE_DATE":"2023-05-11 00:00:00","EXPLAIN":"3家机构买入,成功率42.04%","CLOSE_PRICE":17.86,"CHANGE_RATE":9.9754,"BILLBOARD_NET_AMT":106173086.69,"BILLBOARD_BUY_AMT":360252150.24,"BILLBOARD_SELL_AMT":254079063.55,"BILLBOARD_DEAL_AMT":614331213.79,"ACCUM_AMOUNT":1915938459,"DEAL_NET_RATIO":5.541570826101,"DEAL_AMOUNT_RATIO":32.064245639218,"TURNOVERRATE":7.0992,"FREE_MARKET_CAP":27861906334.72,"EXPLANATION":"日涨幅偏离值达到7%的前5只证券","D1_CLOSE_ADJCHRATE":null,"D2_CLOSE_ADJCHRATE":null,"D5_CLOSE_ADJCHRATE":null,"D10_CLOSE_ADJCHRATE":null,"SECURITY_TYPE_CODE":"058001001"},{"SECURITY_CODE":"000617","SECUCODE":"000617.SZ","SECURITY_NAME_ABBR":"中油资本","TRADE_DATE":"2023-05-11 00:00:00","EXPLAIN":"主力做T,成功率19.95%","CLOSE_PRICE":8.9,"CHANGE_RATE":-10.0101,"BILLBOARD_NET_AMT":-9475597.81999999,"BILLBOARD_BUY_AMT":407172405.39,"BILLBOARD_SELL_AMT":416648003.21,"BILLBOARD_DEAL_AMT":823820408.6,"ACCUM_AMOUNT":5181716882,"DEAL_NET_RATIO":-0.182865988933,"DEAL_AMOUNT_RATIO":15.898599390132,"TURNOVERRATE":4.4745,"FREE_MARKET_CAP":112514503803.1,"EXPLANATION":"日跌幅偏离值达到7%的前5只证券","D1_CLOSE_ADJCHRATE":null,"D2_CLOSE_ADJCHRATE":null,"D5_CLOSE_ADJCHRATE":null,"D10_CLOSE_ADJCHRATE":null,"SECURITY_TYPE_CODE":"058001001"}],"count":65},"success":true,"message":"ok","code":0}'''

data = json.loads(response)
df_data = pd.DataFrame(data)["result"]["data"]

大师看看,我的这个怎么不能形成数据帧呢?怎么修改?求帮忙,谢谢
最佳答案
2023-5-12 08:45:35
问题出在你在使用 pd.DataFrame() 函数时只选择了 "result" 中的 "data" 键的值,而没有选择 "count" 键的值。由于 "count" 键的值不是一个列表,所以不能作为数据帧的一部分。你可以将 "count" 键的值也加入到数据框中,例如:
import jsonimport pandas as pdresponse = '''{"version":"1b6e727c2b13c5a769f82ec8886be915","result":{"pages":2,"data":[{"SECURITY_CODE":"000021","SECUCODE":"000021.SZ","SECURITY_NAME_ABBR":"深科技","TRADE_DATE":"2023-05-11 00:00:00","EXPLAIN":"3家机构买入,成功率42.04%","CLOSE_PRICE":17.86,"CHANGE_RATE":9.9754,"BILLBOARD_NET_AMT":106173086.69,"BILLBOARD_BUY_AMT":360252150.24,"BILLBOARD_SELL_AMT":254079063.55,"BILLBOARD_DEAL_AMT":614331213.79,"ACCUM_AMOUNT":1915938459,"DEAL_NET_RATIO":5.541570826101,"DEAL_AMOUNT_RATIO":32.064245639218,"TURNOVERRATE":7.0992,"FREE_MARKET_CAP":27861906334.72,"EXPLANATION":"日涨幅偏离值达到7%的前5只证券","D1_CLOSE_ADJCHRATE":null,"D2_CLOSE_ADJCHRATE":null,"D5_CLOSE_ADJRATE":null,"D10_CLOSE_ADJCHRATE":null,"SECURITY_TYPE_CODE":"058001001"},{"SECURITY_CODE":"000617","SECUCODE":"000617.SZ","SECURITY_NAME_ABBR":"中油资本","TRADE_DATE":"2023-05-11 00:00:00","EXPLAIN":"主力做T,成功率19.95%","CLOSE_PRICE":8.9,"CHANGE_RATE":-10.0101,"BILLBOARD_NET_AMT":-9475597.81999999,"BILLBOARD_BUY_AMT":407172405.39,"BILLBOARD_SELL_AMT":416648003.21,"BILLBOARD_DEAL_AMT":823820408.6,"ACCUM_AMOUNT":5181716882,"DEAL_NET_RATIO":-0.182865988933,"DEAL_AMOUNT_RATIO":15.898599390132,"TURNOVERRATE":4.4745,"FREE_MARKET_CAP":112514503803.1,"EXPLANATION":"日跌幅偏离值达到7%的前5只证券","D1_CLOSE_ADJCHRATE":null,"D2_CLOSE_ADJCHRATE":null,"D5_CLOSE_ADJRATE":null,"D10_CLOSE_ADJCHRATE":null,"SECURITY_TYPE_CODE":"058001001"}],"count":65},"success":true,"message":"ok","code":0}'''data = json.loads(response)df_data = pd.DataFrame(data["result"]["data"] + [data["result"]["count"]])
这样就能成功生成一个数据框,其中最后一行是 "count" 的值。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-5-12 08:45:35 | 显示全部楼层    本楼为最佳答案   
问题出在你在使用 pd.DataFrame() 函数时只选择了 "result" 中的 "data" 键的值,而没有选择 "count" 键的值。由于 "count" 键的值不是一个列表,所以不能作为数据帧的一部分。你可以将 "count" 键的值也加入到数据框中,例如:
import jsonimport pandas as pdresponse = '''{"version":"1b6e727c2b13c5a769f82ec8886be915","result":{"pages":2,"data":[{"SECURITY_CODE":"000021","SECUCODE":"000021.SZ","SECURITY_NAME_ABBR":"深科技","TRADE_DATE":"2023-05-11 00:00:00","EXPLAIN":"3家机构买入,成功率42.04%","CLOSE_PRICE":17.86,"CHANGE_RATE":9.9754,"BILLBOARD_NET_AMT":106173086.69,"BILLBOARD_BUY_AMT":360252150.24,"BILLBOARD_SELL_AMT":254079063.55,"BILLBOARD_DEAL_AMT":614331213.79,"ACCUM_AMOUNT":1915938459,"DEAL_NET_RATIO":5.541570826101,"DEAL_AMOUNT_RATIO":32.064245639218,"TURNOVERRATE":7.0992,"FREE_MARKET_CAP":27861906334.72,"EXPLANATION":"日涨幅偏离值达到7%的前5只证券","D1_CLOSE_ADJCHRATE":null,"D2_CLOSE_ADJCHRATE":null,"D5_CLOSE_ADJRATE":null,"D10_CLOSE_ADJCHRATE":null,"SECURITY_TYPE_CODE":"058001001"},{"SECURITY_CODE":"000617","SECUCODE":"000617.SZ","SECURITY_NAME_ABBR":"中油资本","TRADE_DATE":"2023-05-11 00:00:00","EXPLAIN":"主力做T,成功率19.95%","CLOSE_PRICE":8.9,"CHANGE_RATE":-10.0101,"BILLBOARD_NET_AMT":-9475597.81999999,"BILLBOARD_BUY_AMT":407172405.39,"BILLBOARD_SELL_AMT":416648003.21,"BILLBOARD_DEAL_AMT":823820408.6,"ACCUM_AMOUNT":5181716882,"DEAL_NET_RATIO":-0.182865988933,"DEAL_AMOUNT_RATIO":15.898599390132,"TURNOVERRATE":4.4745,"FREE_MARKET_CAP":112514503803.1,"EXPLANATION":"日跌幅偏离值达到7%的前5只证券","D1_CLOSE_ADJCHRATE":null,"D2_CLOSE_ADJCHRATE":null,"D5_CLOSE_ADJRATE":null,"D10_CLOSE_ADJCHRATE":null,"SECURITY_TYPE_CODE":"058001001"}],"count":65},"success":true,"message":"ok","code":0}'''data = json.loads(response)df_data = pd.DataFrame(data["result"]["data"] + [data["result"]["count"]])
这样就能成功生成一个数据框,其中最后一行是 "count" 的值。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-12-28 03:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表