我正在尝试将csv文件读入数据帧,以便使用生成的sql select语句中的字段。数据如下所示:
0 1 2 3 4 5 6
0 status1 A0 NaN NaN 3 Customer1 customer Id
1 status2 A1 NaN NaN 3 Customer2 customer Id
2 status3 A2 NaN 1253 3 Customer3 customer Id
3 status4 A3 27.0 L0 12 Customer4 customer Id
4 status5 A3 30.0 NaN 12 Customer5 customer Id
当我遍历每一行并尝试将字段插入字符串时:
for row in M:
print("(Select '{}' as disposition, '{}' as category_code, '{}' as status_code, '{:02f}' as Payer_reason_code, {} as precedence, {} as source_id, '{}' as reco_id) union"
.format(M[row][0], M[row][1], M[row][2], M[row][3], M[row][4], M[row][5], M[row][6]))
抛出此错误:
第14行,在 print(“(选择'{}'作为处置,'{}'作为category_code,'{}'作为status_code,'{:02f}'作为Payer_reason_code,{}作为优先,{}作为source_id,'{}'作为reco_id)union“.format(M [row] [0],M [row] [1],M [row] [2],M [row] [3],M [row] [4],M [row ] [5],M [row] [6])) IndexError:用作索引的数组必须是整数(或布尔)类型
如何在numpy 2d数组上循环?
这是完整的脚本:
import pandas as pd
import os
import numpy as np
path = '../resources'
X = pd.read_csv('../resources/data.csv', header=None).as_matrix()
for row in X:
print("(Select '{}' as disposition, '{}' as category_code, '{}' as status_code, '{:02f}' as Payer_reason_code, {} as precedence, {} as source_id, '{}' as reco_id) union".format(X[row][0], X[row][1], X[row][2], X[row][3], X[row][4], X[row][5], X[row][6]))
答案 0 :(得分:0)
你不能遍历这样的数据帧。
for i, row in M.iterrows():
print("(Select '{}' as disposition, '{}' as category_code, '{}' as status_code, '{:02f}' as Payer_reason_code, {} as precedence, {} as source_id, '{}' as reco_id) union"
.format(row[0], row[1], row[2], row[3], row[4], row[5], row[6]))