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

报错: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日

悬赏问题

  • ¥15 latex投稿显示click download
  • ¥15 请问读取环境变量文件失败是什么原因?
  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?