Home > Software engineering >  Why Can't I Iterate Over Dataframe Object in Python?
Why Can't I Iterate Over Dataframe Object in Python?

Time:10-04

I'm trying to export a bunch of dataframes to SQL tables skipping over ones with errors but am getting this error: "TypeError: 'DataFrame' objects are mutable, thus they cannot be hashed."

dict = {df1:'tableName1',
        df2:'tableName2',
        df3:'tableName3'}

for df in dict:
  try:
    df.to_sql(dict[df],engine,index=False,method=functools.partial(pd_writer,quote_identifiers=False),if_exists = 'append')
  except ProgrammingError:
    pass

CodePudding user response:

The error is not related to sql. Your DataFrame object cannot be the key of a dictionary.

CodePudding user response:

You can't use a dataframe as the key of a dictionary. Use a list of tuples rather than a dictionary.

dataframes = (
    ('tableName1', df1),
    ('tableName2', df2),
    ('tableName3', df3)
)

for table, df in dataframes:
    df.to_sql(table,engine,index=False,method=functools.partial(pd_writer,quote_identifiers=False),if_exists = 'append')

If you really need a dictionary, you could flip it around and use the table names as the keys, then use

for table, df in dataframes.items():

CodePudding user response:

As the error states you are trying to index using a mutable object. Mutable objects are not hashable. In Pandas DataFrame objects are more like tables, and they are mutable, thus they cannot be used as indices.

You can see more in this excellent article showing why the error occurs, and how to fix it.

Pandas DataFrame cannot be hashed

  • Related