鱼C论坛

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

[已解决]请大师帮忙形成pd数据帧,谢谢

[复制链接]
发表于 2023-4-11 11:07:59 | 显示全部楼层 |阅读模式

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

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

x
data = '''Query112307719530320351087_1671530759948({"rc":0,"rt":22,"svr":181216787,"lt":1,"full":0,"dlmkts":"","data":{"code":"000001","market":1,"name":"上证指数","klines":["2022-11-10,-34451102208.0,28253458432.0,6197641216.0,-18004312064.0,-16446790144.0,-4.09,3.35,0.74,-2.14,-1.95,3036.13,-0.39,10908.55,-1.33","2022-11-11,2016566784.0,12980424704.0,-14996992000.0,-10999246848.0,13015813632.0,0.16,1.06,-1.23,-0.90,1.06,3087.29,1.69,11139.77,2.12","2022-11-14,-30351907584.0,23198273536.0,7153635328.0,-14855168000.0,-15496739584.0,-2.84,2.17,0.67,-1.39,-1.45,3083.40,-0.13,11113.46,-0.24]}});'''
data_str = re.findall(r'(\{.*\})',response)[0] #提出数据
data_json = json.loads(data_str)['data']['klines']
data_list = data_json.split('","',')    #?????
df = pd.DataFrame([data_json])
print(df)

出来只一列,我需要以日期为界成一个列表,出现三行多列
即要把日期前面","》这个为分界,行成多个列表
请大师指导修改代码,谢谢
最佳答案
2023-4-11 11:12:47
从给定的数据来看,您需要将data_json变量中的字符串分割成多个列表,并将这些列表作为DataFrame的行。这是一个修改后的代码:
import re
import json
import pandas as pd

data = '''Query112307719530320351087_1671530759948({"rc":0,"rt":22,"svr":181216787,"lt":1,"full":0,"dlmkts":"","data":{"code":"000001","market":1,"name":"上证指数","klines":["2022-11-10,-34451102208.0,28253458432.0,6197641216.0,-18004312064.0,-16446790144.0,-4.09,3.35,0.74,-2.14,-1.95,3036.13,-0.39,10908.55,-1.33","2022-11-11,2016566784.0,12980424704.0,-14996992000.0,-10999246848.0,13015813632.0,0.16,1.06,-1.23,-0.90,1.06,3087.29,1.69,11139.77,2.12","2022-11-14,-30351907584.0,23198273536.0,7153635328.0,-14855168000.0,-15496739584.0,-2.84,2.17,0.67,-1.39,-1.45,3083.40,-0.13,11113.46,-0.24]}});'''
data_str = re.findall(r'(\{.*\})', data)[0]  # 提出数据
data_json = json.loads(data_str)['data']['klines']

# 分割字符串,创建多个列表
data_list = [x.split(',') for x in data_json]

# 将列表作为DataFrame的行
df = pd.DataFrame(data_list)

print(df)

现在,您的DataFrame将包含3行和15列,如您所期望的。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-4-11 11:12:47 | 显示全部楼层    本楼为最佳答案   
从给定的数据来看,您需要将data_json变量中的字符串分割成多个列表,并将这些列表作为DataFrame的行。这是一个修改后的代码:
import re
import json
import pandas as pd

data = '''Query112307719530320351087_1671530759948({"rc":0,"rt":22,"svr":181216787,"lt":1,"full":0,"dlmkts":"","data":{"code":"000001","market":1,"name":"上证指数","klines":["2022-11-10,-34451102208.0,28253458432.0,6197641216.0,-18004312064.0,-16446790144.0,-4.09,3.35,0.74,-2.14,-1.95,3036.13,-0.39,10908.55,-1.33","2022-11-11,2016566784.0,12980424704.0,-14996992000.0,-10999246848.0,13015813632.0,0.16,1.06,-1.23,-0.90,1.06,3087.29,1.69,11139.77,2.12","2022-11-14,-30351907584.0,23198273536.0,7153635328.0,-14855168000.0,-15496739584.0,-2.84,2.17,0.67,-1.39,-1.45,3083.40,-0.13,11113.46,-0.24]}});'''
data_str = re.findall(r'(\{.*\})', data)[0]  # 提出数据
data_json = json.loads(data_str)['data']['klines']

# 分割字符串,创建多个列表
data_list = [x.split(',') for x in data_json]

# 将列表作为DataFrame的行
df = pd.DataFrame(data_list)

print(df)

现在,您的DataFrame将包含3行和15列,如您所期望的。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-11-14 03:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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