Home > OS >  How to solve Unhashable type: 'DataFrame'? when saving to SQL?
How to solve Unhashable type: 'DataFrame'? when saving to SQL?

Time:05-10

I have the following DataFrame:

df_tweets = pd.DataFrame({'source': ['Twitter Web Client', 'Twitter Web Client', 'Twitter Web Client', 'Twitter Web Client', 'Twitter Web Client'], 
                          'id_str': [6971079756, 6312794445, 6090839867, 5775731054, 5364614040], 
                          'text': ['From Donald Trump: Wishing everyone a wonderful holiday & a happy, healthy, prosperous New Year. Let’s think like champions in 2010!', 
                                   'My International Tower in Chicago ranked 6th tallest building in world by Council on Tall Buildings & Urban Habitat http://bip.ly/sqvQq', 
                                   'Wishing you and yours a very Happy and Bountiful Thanksgiving!', 
                                   "Donald Trump Partners with TV1 on New Reality Series Entitled, Omarosa's Ultimate Merger: http://turl.com/yk5m3lc", 
                                   '--Work has begun, ahead of schedule, to build the greatest golf course in history: Trump International – Scotland.'], 
                          'created_at': ['2009-12-23T17:38:18Z', '2009-12-03T19:39:09Z', '2009-11-26T19:55:38Z', '2009-11-16T21:06:10Z', '2009-11-02T14:57:56Z'], 
                          'retweet_count': [28, 33, 13, 5, 7], 
                          'in_reply_to_user_id_str': [np.nan, np.nan, np.nan, np.nan, np.nan], 
                          'favorite_count': [12, 6, 11, 3, 6], 
                          'is_retweet': [False, False, False, False, False], 
                          'key': [1, 2, 3, 4, 5]})

And I want to save it to a database (SQLite). So I followed these steps:

engine = create_engine('sqlite:///tweets.db', echo=True)
sqlite_connection = engine.connect()
df_tweets.to_sql(df_tweets, sqlite_connection, if_exists='fail')

But I get this error:

TypeError: unhashable type: 'DataFrame'

I tried to solve it looking for solutions on Internet, and I've found that it could be that one of my columns is a list (which is unhasheable). So I tried it to discover if one of the columns was a list or a dict:

df_tweets.applymap(lambda x: isinstance(x, dict) or isinstance(x, list)).all()
source                     False
id_str                     False
text                       False
created_at                 False
retweet_count              False
in_reply_to_user_id_str    False
favorite_count             False
is_retweet                 False
key                        False
dtype: bool

But no, I don't see that any column is a list. I'm stuck at solving this problem, please could you guide me what I have to do?

CodePudding user response:

I think you are not using correctly the to_sql, as you can see on this screen : enter image description here

'products' is the name of the table you want to insert your data and not your dataframe

  • Related