如何解决Unhashable type: 'DataFrame'?保存到 SQL 时?

问题描述 投票:0回答:2

我有以下数据框:

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'

我尝试在互联网上寻找解决方案来解决这个问题,我发现可能是我的其中一个专栏是一个列表(不可散列)。所以我尝试找出其中一列是列表还是字典:

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

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

python pandas sqlite
2个回答
1
投票

我认为您没有正确使用

to_sql
,正如您在此屏幕上看到的:

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


0
投票

而不是: df_tweets.to_sql(df_tweets, sqlite_connection, if_exists='fail'), 在表名周围添加引号: df_tweets.to_sql('df_tweets', sqlite_connection, if_exists='fail'),然后错误将被修复。

© www.soinside.com 2019 - 2024. All rights reserved.