如何解决不可用的类型:' dataframe&#x27 ;?保存到SQL时?

发布于 2025-01-27 07:15:35 字数 2420 浏览 3 评论 0原文

我有以下数据框架:

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上寻找解决方案的问题,并且我发现我的一列可能是列表(这是不可用的)。因此,我尝试它发现其中一列是列表还是一个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

但是,我看不到任何列是列表。我陷入了解决这个问题的困扰,请您指导我要做的事情吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

も让我眼熟你 2025-02-03 07:15:35

我认为您无法正确使用to_sql,正如您在此屏幕上看到的那样: “在此处输入映像”

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

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

唠甜嗑 2025-02-03 07:15:35

而不是:

df_tweets.to_sql(df_tweets, sqlite_connection, if_exists='fail')

添加表格名称周围的报价:

df_tweets.to_sql('df_tweets', sqlite_connection, if_exists='fail')

然后将修复错误。

Instead of:

df_tweets.to_sql(df_tweets, sqlite_connection, if_exists='fail')

add quotation around your table name:

df_tweets.to_sql('df_tweets', sqlite_connection, if_exists='fail')

Then the error will be fixed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文