如何解决不可用的类型:' dataframe&#x27 ;?保存到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上寻找解决方案的问题,并且我发现我的一列可能是列表(这是不可用的)。因此,我尝试它发现其中一列是列表还是一个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您无法正确使用
to_sql
,正如您在此屏幕上看到的那样:'products'是您要插入数据而不是插入数据的名称您的数据框架
I think you are not using correctly the
to_sql
, as you can see on this screen :'products' is the name of the table you want to insert your data and not your dataframe
而不是:
添加表格名称周围的报价:
然后将修复错误。
Instead of:
add quotation around your table name:
Then the error will be fixed.