简体   繁体   English

熊猫数据框未附加

[英]Pandas Data Frame not Appending

I am trying to append dataframes via for loop.我正在尝试通过 for 循环附加数据帧。

CODE代码

def redshift_to_pandas(sql_query,**kwargs):
# pass a sql query and return a pandas dataframe
 cur.execute(sql_query)
 columns_list = [desc[0] for desc in cur.description]
 data = pd.DataFrame(cur.fetchall(),columns=columns_list)
 return data

Input - all_schema = [('backup')]输入 - all_schema = [('backup')]

Loop -环形 -

try:
 if len(all_schema) == 0:
  raise inputError("The Input has no schema selected. EXITING")
 else:
  modified_schemadf=pd.DataFrame(columns=['columns_name','status'])
  
  for i in range(len(all_schema)):
    #print (redshift_to_pandas("select '"+all_schema[i]+"' as columns_name,(select exists ( select distinct table_schema from information_schema.tables where table_schema like '%"+all_schema[i]+"')) as status",mechanism='append'))
    modified_schemadf.append(redshift_to_pandas("select '"+all_schema[i]+"' as columns_name,(select exists ( select distinct table_schema from information_schema.tables where table_schema like '%"+all_schema[i]+"')) as status",mechanism='append'))
    print (modified_schemadf)

except inputError as e:
   print(e.message)
   logger.error("UNEXPECTED INPUT FOUND, Please check the I/P List . EXITING")
  
print (modified_schemadf)

I feel the issue is very obvious but i dont seem to find the issue.我觉得问题很明显,但我似乎没有找到问题。

Here is the o/p -这是o/p -

结果图片

So the the first print ( commented out ), does return me the correct result.所以第一个打印(注释掉)确实返回了正确的结果。 the next steps ie appending the result to the declared dataframe ( name - modified_schemadf) is the problem area.接下来的步骤,即将结果附加到声明的数据帧(名称 - modified_schemadf)是问题区域。 When i print its value , it still throws a empty dataframe.当我打印它的值时,它仍然抛出一个空的数据帧。 For some reason the appending isnt happening.由于某种原因,追加没有发生。

When the code enters else , ie when the input is legit, there will be empty dataframe created called modified_schemadf.当代码进入else 时,即当输入合法时,将创建一个名为 modified_schemadf 的空数据框。 To this empty dataframe, there will be as many number of appends as there are inputs.对于这个空的数据帧,将有与输入一样多的追加。

Thanks in Advance.提前致谢。

Please dont mind the indentations, copying might have affected them.请不要介意缩进,复制可能会影响它们。

Isn't the issue just that you don't assign the appended dataframe?问题不只是您没有分配附加的数据框吗? Try changing this line尝试更改此行

modified_schemadf.append(redshift_to_pandas("select '"+all_schema[i]+"' as columns_name,(select exists ( select distinct table_schema from information_schema.tables where table_schema like '%"+all_schema[i]+"')) as status",mechanism='append'))

to this line到这一行

modified_schemadf = modified_schemadf.append(redshift_to_pandas("select '"+all_schema[i]+"' as columns_name,(select exists ( select distinct table_schema from information_schema.tables where table_schema like '%"+all_schema[i]+"')) as status",mechanism='append'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM