云云豆酱 2022-11-09 19:54 采纳率: 75%
浏览 99
已结题

报错:TypeError: invalid type comparison,百度试了几个方法还是不行,请问这怎么解决。

要求:使用loc,和 iloc,实现条件切片读取数据源“meal_order_detail.xlsx"或"meal_order_detaill.aql",使用条件表达式,返回某一特定条件下的所有值。


import pandas as pd

detail = pd.read_excel('D:/AAA学习/数据分析/2022.11.9/meal_order_detail.xlsx')
order_id = detail['order_id']
dishes_name = detail.dishes_name
dishes_name1 = detail.loc[:, 'dishes_name']
dishes_name2 = detail.iloc[:, 3]
orderDish1 = detail.loc[:, ['order_id', 'dishes_name']]
orderDish2 = detail.iloc[:, [1, 3]]
# loc内部传入表达式
print('detail中order_id为458的dishes_name为: \n', detail.loc[detail['order_id'] == '1', ['order_id', 'dishes_name']])

img


请问这怎么解决

  • 写回答

2条回答 默认 最新

  • 云云豆酱 2022-11-09 21:05
    关注

    用数据库却可以,这是为什么呢

    
    import pandas as pd
    import numpy
    from sqlalchemy import create_engine
    engine = create_engine('mysql+pymysql://root:root@127.0.0.1:\
    3306/testdb?charset=utf8')
    detail = pd.read_sql_table('meal_order_detail1',con = engine)
    order_id = detail['order_id']
    dishes_name = detail.dishes_name
    print('detail中order_id为458的dishes_name为: \n', detail.loc[detail['order_id']=='458',['order_id','dishes_name']])
    print('detail中order_id为458的第1,5列数据为: \n', detail.iloc[(detail['order_id']=='458').values,[1,5]])
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月24日
  • 已采纳回答 11月16日
  • 修改了问题 11月9日
  • 创建了问题 11月9日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效