如何解决无法散列的类型:'DataFrame'?保存到 SQL 时?

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

我有以下数据框:

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]})

我想将它保存到数据库 (SQLite)。所以我遵循了这些步骤:

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

但是我得到这个错误:

TypeError: unhashable type: 'DataFrame'

我试图解决它,在 Internet 上寻找解决方案,我发现我的一个专栏可能是一个列表(不可散列)。所以我试着用它来发现其中一列是列表还是字典:

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

但是不,我没有看到任何列都是列表。我一直在解决这个问题,请问您能指导我该怎么做吗?

我认为您没有正确使用 to_sql,如您在屏幕上所见:

'products' 是您要插入数据的 table 的名称,而不是数据框