1241, '操作数应包含 1 列' pandas to_sql

Posted

技术标签:

【中文标题】1241, \'操作数应包含 1 列\' pandas to_sql【英文标题】:1241, 'Operand should contain 1 column(s)' pandas to_sql1241, '操作数应包含 1 列' pandas to_sql 【发布时间】:2021-03-26 11:36:36 【问题描述】:

我在尝试将数据帧推送到 sql 时遇到一个奇怪的 SQL 错误。我的df 只是一个我试图推送到我的 SQL 数据库的 pandas 数据框。我以前从未遇到过问题,并且错误消息对于问题所在的信息非常无用。错误类似于:mysql - Operand should contain 1 column(s) 虽然我用的是python。

我的df 中的.head() 看起来像这样,要推送的代码和我的错误看起来像这样:

                                   uuid  ...                             source.imageUrls.large
0  fe768572-c96c-4936-8138-3ac09e75c4cc  ...  https://cityfalconproduction.blob.core.windows...
1  eb8bddac-56f7-4ed6-a214-325a97ce6104  ...  https://cityfalconproduction.blob.core.windows...
2  a89899e0-e34b-416e-a8b3-387fb7293b90  ...  https://cityfalconproduction.blob.core.windows...
3  9340719e-68c8-4281-860f-895b3b54644f  ...  https://cityfalconproduction.blob.core.windows...
4  a369b1e7-1496-4614-b866-b8d48cce9713  ...  https://cityfalconproduction.blob.core.windows...
[5 rows x 23 columns]

df.to_sql('news', con=engine, if_exists='append', index=False)

--> sqlalchemy.exc.OperationalError: (MySQLdb._exceptions.OperationalError) (1241, 'Operand should contain 1 column(s)')
[SQL: INSERT INTO news_articlesz (`index`, uuid, `publishTime`, title, description, `assetTags`, `searchTags`, category, url, `imageUrls`, sentiment, lang, `cityfalconScore`, `duplicatesCount`, paywall, `registrationRequired`, cityfalcon_permalink, `source.name`, `source.brandName`, `source.imageUrl`, `source.imageUrls.thumb`, `source.imageUrls.small`, `source.imageUrls.medium`, `source.imageUrls.large`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)]
[parameters: ((0, 'fe768572-c96c-4936-8138-3ac09e75c4cc', '2021-04-08T15:49:00.000Z', 'Microsoft approaches $2 trillion in market value as its stock hits record high', "Microsoft is closing in on a $2 trillion market capitalization. It would be only the second US company after Apple to pass that threshold. The computing titan's", ['Apple Inc', 'AAPL NASDAQ', 'AAPL US'], ['AAPL'], 'major_publication', 'https://www.businessinsider.in/stock-market/news/microsoft-approaches-2-trillion-in-market-value-as-its-stock-hits-record-high/articleshow/81973984.cms?utm_campaign=cityfalcon&utm_medium=cityfalcon&utm_source=cityfalcon', ['https://www.businessinsider.in/photo/81973984/microsoft-approaches-2-trillion-in-market-value-as-its-stock-hits-record-high.jpg?imgsize=253771'], None, 'en', 67, 11, 0, 0, 'https://www.cityfalcon.com/news/directory/stocks/apple-inc-aapl', 'businessinsider.in', 'Business Insider India', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/3333/small.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/3333/thumb.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/3333/small.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/3333/medium.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/3333/large.png'), (1, 'eb8bddac-56f7-4ed6-a214-325a97ce6104', '2021-04-13T16:53:09.000Z', 'Epic announces billion-dollar funding round ahead of Apple trial | Engadget', 'Sony has increased its stake in the company with a $200 million investment.', ['Apple Inc', 'AAPL NASDAQ', 'AAPL US'], ['AAPL'], 'other_publication', 'https://www.engadget.com/epic-billion-dollar-funding-round-sony-app-store-lawsuit-apple-165309621.html?utm_campaign=cityfalcon&utm_medium=cityfalcon&utm_source=cityfalcon', ['https://s.yimg.com/os/creatr-uploaded-images/2021-04/b3369ea0-9c75-11eb-bf4f-12ab7ed0bb34'], None, 'en', 65, 25, 0, 0, 'https://www.cityfalcon.com/news/directory/stocks/apple-inc-aapl', 'engadget.com', 'Engadget', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/2955/small.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/2955/thumb.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/2955/small.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/2955/medium.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/2955/large.png'), (2, 'a89899e0-e34b-416e-a8b3-387fb7293b90', '2021-04-11T17:20:09.198Z', 'Microsoft in talks to buy Burlington AI and technology firm Nuance Communications, sources say - The Boston Globe', "The deal would value Nuance, which laid the groundwork for the technology used in Apple's Siri voice software, at about $16 billion, according to data compiled by Bloomberg.", ['Apple Inc', 'AAPL NASDAQ', 'AAPL US'], ['AAPL'], 'other_publication', 'https://www.bostonglobe.com/2021/04/11/business/microsoft-talks-buy-burlington-ai-technology-firm-nuance-communications/?camp=bg%3Abrief%3Arss%3Afeedly&rss_id=feedly_rss_brief&s_campaign=bostonglobe%3Asocialflow%3Atwitter&utm_campaign=cityfalcon&utm_medium=cityfalcon&utm_source=cityfalcon', ['https://bostonglobe-prod.cdn.arcpublishing.com/resizer/KujNh-uiIXPqgdfA0HuVA5z2cWg=/506x0/cloudfront-us-east-1.images.arcpublishing.com/bostonglobe/XKNGLJFHLGSTCOZUB4IGSBSLRI.jpg'], None, 'en', 62, 0, 0, 0, 'https://www.cityfalcon.com/news/directory/stocks/apple-inc-aapl', 'bostonglobe.com', 'The Boston Globe', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/1679073/small.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/1679073/thumb.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/1679073/small.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/1679073/medium.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/1679073/large.png'), (3, '9340719e-68c8-4281-860f-895b3b54644f', '2021-04-13T16:32:32.000Z', 'Apple confirms Spring Loaded April 20 event, new iPad Pro expected', 'Apple is likely to launch new iPad Pro model at its April 20 event.', ['Apple Inc', 'AAPL NASDAQ', 'AAPL US'], ['AAPL'], 'other_publication', 'https://www.indiatoday.in/technology/news/story/apple-confirms-spring-loaded-april-20-event-new-ipad-pro-expected-1790602-2021-04-13?utm_campaign=cityfalcon&utm_medium=cityfalcon&utm_source=cityfalcon', ['https://akm-img-a-in.tosshub.com/indiatoday/images/story/202104/apple-event-Spring-647x363.jpeg?3g.1QDt3MEp9uerkfC41JPKpFPWPl5vl'], '35.06', 'en', 61, 5, 0, 0, 'https://www.cityfalcon.com/news/directory/stocks/apple-inc-aapl', 'indiatoday.in', 'India Today', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/13110/small.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/13110/thumb.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/13110/small.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/13110/medium.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/13110/large.png'), (4, 'a369b1e7-1496-4614-b866-b8d48cce9713', '2021-04-10T05:11:05.000Z', 'Apple locks in users by keeping iMessage ios-only, Epic says', "Apple apparently thinks it is one of the reasons people don't abandon iOS for android.", ['Apple Inc', 'AAPL NASDAQ', 'AAPL US'], ['AAPL'], 'other_publication', 'https://www.pocket-lint.com/apps/news/apple/156489-apple-locks-in-users-by-keeping-imessage-ios-only-epic-court-filings-show?utm_campaign=cityfalcon&utm_medium=cityfalcon&utm_source=cityfalcon', ['https://cdn.pocket-lint.com/r/s/1200x630/assets/images/156489-homepage-news-apple-won-t-make-imessage-for-android-to-lock-in-its-users-epic-filings-show-image1-pk7xjh3i4m.jpg'], '-44.5', 'en', 61, 4, 0, 0, 'https://www.cityfalcon.com/news/directory/stocks/apple-inc-aapl', 'pocket-lint.com', 'Pocket-lint', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/4945/small.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/4945/thumb.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/4945/small.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/4945/medium.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/4945/large.png'), (5, 'd4988c5b-7cd3-43c9-91a6-2367d4ca2cc5', '2021-04-07T17:30:20.000Z', 'Apple Launches New Find My Network For Third Party Accessory Makers', 'Along with the release of the new iOS 14.5 beta, Apple announced its new Find My Network accessory program for third-party accessory makers.', ['Apple Inc', 'AAPL NASDAQ', 'AAPL US'], ['AAPL'], 'other_publication', 'https://www.iphonehacks.com/2021/04/apple-find-my-network-accessory-makers.html?utm_campaign=cityfalcon&utm_medium=cityfalcon&utm_source=cityfalcon', ['https://www.iphonehacks.com/wp-content/uploads/2021/04/apple-find-my-network.jpg'], '50.0', 'en', 61, 2, 0, 0, 'https://www.cityfalcon.com/news/directory/stocks/apple-inc-aapl', 'iphonehacks.com', 'iPhone Hacks', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/19035/small.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/19035/thumb.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/19035/small.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/19035/medium.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/19035/large.png'), (6, 'da949268-0944-4c97-8cca-1691af972f92', '2021-04-07T18:38:01.000Z', 'Apple releases seventh iOS 14.5, watchOS 7.4, and tvOS 14.5 betas', 'Today, Apple released new developer betas of the next versions of iOS, iPadOS, watchOS, and tvOS. The big new feature is that you can unlock your iPhone with an Apple Watch, and more.', ['Apple Inc', 'AAPL NASDAQ', 'AAPL US'], ['AAPL'], 'other_publication', 'https://www.neowin.net/news/apple-releases-seventh-ios-145-watchos-74-and-tvos-145-betas/?utm_campaign=cityfalcon&utm_medium=cityfalcon&utm_source=cityfalcon', ['https://cdn.neow.in/news/images/uploaded/2018/09/1537561292_img_4181_story.jpg'], '32.14', 'en', 61, 2, 0, 0, 'https://www.cityfalcon.com/news/directory/stocks/apple-inc-aapl', 'neowin.net', 'Neowin', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/1995/small.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/1995/thumb.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/1995/small.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/1995/medium.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/1995/large.png'), (7, '87d05ce2-0742-4010-8ac5-fb779cddbcfd', '2021-04-09T21:16:56.000Z', 'Apple decides to skip upcoming Senate antitrust hearing, drawing criticism | AppleInsider', 'U.S. lawmakers are urging Apple to reconsider its decision not to testify in an upcoming Senate subcommittee hearing on antitrust issues in mobile app stores.', ['Apple Inc', 'AAPL NASDAQ', 'AAPL US'], ['AAPL'], 'other_publication', 'https://appleinsider.com/articles/21/04/09/apple-decides-to-skip-upcoming-senate-antitrust-hearing-drawing-criticism?utm_campaign=cityfalcon&utm_medium=cityfalcon&utm_source=cityfalcon', ['https://photos5.appleinsider.com/gallery/41303-80105-US-Capitol-xl.jpg'], '-62.33', 'en', 61, 2, 0, 0, 'https://www.cityfalcon.com/news/directory/stocks/apple-inc-aapl', 'appleinsider.com', 'AppleInsider', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/1708/small.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/1708/thumb.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/1708/small.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/1708/medium.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/1708/large.png')  ... displaying 10 of 82 total bound parameter sets ...  (80, '4d6f3d0f-d5fc-44b2-b199-be8de71b35ce', '2021-04-10T16:49:26.000Z', 'Apple Pulls Out of App Store Hearing, Enraging U.S. Senators', '"We strongly urge Apple to reconsider its position and to provide a witness to testify before the Subcommittee in a timely manner."', ['Apple Inc', 'AAPL NASDAQ', 'AAPL US'], ['AAPL'], 'other_publication', 'https://www.iphoneincanada.ca/news/apple-pulls-out-of-app-store-hearing-enraging-u-s-senators/?utm_campaign=cityfalcon&utm_medium=cityfalcon&utm_source=cityfalcon', ['https://cdn.iphoneincanada.ca/wp-content/uploads/2020/06/app-store.jpg'], '-55.9', 'en', 50, 3, 0, 0, 'https://www.cityfalcon.com/news/directory/stocks/apple-inc-aapl', 'iphoneincanada.ca', 'iPhone in Canada', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/20178/small.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/20178/thumb.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/20178/small.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/20178/medium.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/20178/large.png'), (81, '5601ab6f-5727-48e8-b966-e628b10a8577', '2021-04-13T09:54:03.000Z', 'Siri spills the beans on Apple’s 4/20 event (BLAZE IT LOL)', 'It’s happening. Probably. Apple is planning to host an event on, April 20, where it might reveal new iPad Pros and (possibly??) its AirTags smart tracker. Take this with a pinch of salt though. This date doesn’t come from an Apple press rel', ['Apple Inc', 'AAPL NASDAQ', 'AAPL US'], ['AAPL'], 'other_publication', 'https://thenextweb.com/news/siri-spills-the-beans-on-apples-4-20-event-blaze-it-lol?utm_campaign=cityfalcon&utm_medium=cityfalcon&utm_source=cityfalcon', ['https://img-cdn.tnwcdn.com/image/plugged?filter_last=1&fit=1280%2C640&url=https%3A%2F%2Fcdn0.tnwcdn.com%2Fwp-content%2Fblogs.dir%2F1%2Ffiles%2F2021%2F04%2Fweed-siri.jpg&signature=4ed1783e579861b6a554b82efd830831'], None, 'en', 50, 0, 0, 0, 'https://www.cityfalcon.com/news/directory/stocks/apple-inc-aapl', 'thenextweb.com', 'The Next Web', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/5382/small.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/5382/thumb.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/5382/small.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/5382/medium.png', 'https://cityfalconproduction.blob.core.windows.net/autotweetmedia/domains/logos/5382/large.png'))]
(Background on this error at: http://sqlalche.me/e/14/e3q8)

这没有意义,因为我有 23 列?数据库也包含这些带有这些标题的确切列,所以我不明白为什么不推送。

我是 mySQL 的新手,很高兴提供任何可能有助于解决问题的数据或结构。

任何问题的迹象都会非常有帮助。谢谢

【问题讨论】:

您确定news 是表还是视图? @BarbarosÖzhan 是的。我什至放弃了它,让它创建一个新的,但它仍然失败。 数据框中是否有任何列是列表、字典、json 等? @eduardoftdo 列本身只是字符串,尽管我确实有包含列表的列值。 minimal reproducible example 甚至是数据库结构以及您在数据框中加载和更改的内容都可以帮助我们理解问题。 【参考方案1】:

当我尝试将包含列表的列推送到不兼容的 SQL 数据库中时出现此错误。为了解决这个问题,我只是将列表转换为字符串,它就可以工作了。

不确定这是否会有所帮助,但这是我解决此特定问题的方法。

操作

【讨论】:

以上是关于1241, '操作数应包含 1 列' pandas to_sql的主要内容,如果未能解决你的问题,请参考以下文章

MySQL 错误 1241:操作数应包含 1 列

java 和 sql 显示 #1241 - 操作数应包含 1 列

如何修复 SQLSTATE[21000]:基数违规:1241 操作数应包含 1 列

#1241 - 操作数应在 phpmyadmin 上的 wamp 中包含 1 列

Pandas groupby 和聚合输出应包括所有原始列(包括未聚合的列)

需要 SQL 查询帮助并显示错误消息操作数应包含 1 列