我正在使用vertica_python从数据库中提取数据。我拉的列是以下列格式的字符串:
[{"id":0,"prediction_type":"CONV_PROBABILITY","calibration_factor":0.906556,"inte cept":-2.410414,"advMatchTypeId":-0.239877,"atsId":-0.135568,"deviceTypeId":0.439130,"dmaCode":-0.251728,"keywordId":0.442240}]
然后我拆分并解析这个sting并按以下格式加载到excel中,每个索引都是一个单元格:
prediction_type CONV_PROBABILIT calibration_factor 0.90655 intercept -2.41041 advMatchTypeId -0.23987 atsId 1.44701 deviceTypeId 0.19701 dmaCode -0.69982 keywordId 0.44224
这是我的问题。字符串没有明确的格式,这意味着,有时我会遗漏字符串中的某些功能,弄乱我的格式。这是一个例子:
intercept -2.41041 advMatchTypeId -0.23987 deviceTypeId 0.37839 dmaCode -0.53552 keywordId 0.44224
intercept -2.41041 advMatchTypeId -0.23987 atsId 0.80708 deviceTypeId -0.19573 dmaCode -0.69982 keywordId 0.44224
如何保留我想要的格式,让上面的例子看起来像这样:
intercept -2.41041 advMatchTypeId -0.23987 deviceTypeId 0.37839 dmaCode -0.53552 keywordId 0.44224
intercept -2.41041 advMatchTypeId -0.23987 atsId 0.80708 deviceTypeId -0.19573 dmaCode -0.69982 keywordId 0.44224
这是我正在使用的代码:
data_all = cur.fetchall()
for i in range(len(data_all)):
col = 0
data_one = ''.join(data_all[i])
raw_coef = data_one.split(',')[1:len(data_all)]
for j in range(len(raw_coef)):
raw = ''.join(raw_coef[j])
raw = re.sub('"|}|{|[|]|', '', raw)[:-1]
raw = raw.split(":")
for k in range(len(raw)):
worksheet.write(i, col, raw[k], align_left)
feature.append(raw[0]) # for unique values
col+=1
我的查询:
cur.execute(
"""
select MODEL_COEF
from
dcf_funnel.ADV_BIDDER_PRICING_LOG
where MODEL_ID = 8960
and DATE(AMP_QUERY_TIMESTAMP) = '11-02-2016'
"""
)
答案 0 :(得分:3)
您可以跳过所有解析并使用pandas:
import pandas
如果它已经是Python中的dicts列表,那么这会将您的查询结果读入DataFrame。
data_all_list = [{"id":0,"prediction_type":"CONV_PROBABILITY","calibration_factor":0.906556,"intercept":-2.410414,"advMatchTypeId":-0.239877,"atsId":-0.135568,"deviceTypeId":0.439130,"dmaCode":-0.251728,"keywordId":0.442240}]
df = pandas.DataFrame(data_all_list)
如果你真的有字符串,可以使用read_json
:
data_all_str = """[{"id":0,"prediction_type":"CONV_PROBABILITY","calibration_factor":0.906556,"intercept":-2.410414,"advMatchTypeId":-0.239877,"atsId":-0.135568,"deviceTypeId":0.439130,"dmaCode":-0.251728,"keywordId":0.442240}]"""
df = pandas.read_json(data_all_str)
进一步的想法使我明白你的data_all
实际上是一个dicts列表列表,如下所示:
data_all_lol = [data_all_list, data_all_list]
在这种情况下,您需要在传递给DataFrame之前连接列表:
df = pandas.DataFrame(sum(data_all_lol, []))
这将以正常的标题+值格式写出:
df.to_csv('filename.csv') # you can also use to_excel
如果您的最终目标只是获取所有功能的方法,那么大熊猫可以使用任意数量的列直接执行此操作,正确处理缺失值:
df.mean()
给出
advMatchTypeId -0.239877
atsId -0.135568
calibration_factor 0.906556
deviceTypeId 0.439130
dmaCode -0.251728
id 0.000000
intercept -2.410414
keywordId 0.442240
注意歧义
在OP中,很难知道data_all
的类型,因为您显示的代码段在文字语法中看起来像一个字典列表,但是您说“我拉的列以字符串形式出现”。< / p>
注意在以下IPython会话中表示输入的方式之间的区别:
In [15]: data_all_str
Out[15]: '[{"id":0,"prediction_type":"CONV_PROBABILITY","calibration_factor":0.906556,"intercept":-2.410414,"advMatchTypeId":-0.239877,"atsId":-0.135568,"deviceTypeId":0.439130,"dmaCode":-0.251728,"keywordId":0.442240}]'
In [16]: data_all_list
Out[16]:
[{'advMatchTypeId': -0.239877,
'atsId': -0.135568,
'calibration_factor': 0.906556,
'deviceTypeId': 0.43913,
'dmaCode': -0.251728,
'id': 0,
'intercept': -2.410414,
'keywordId': 0.44224,
'prediction_type': 'CONV_PROBABILITY'}]