【问题标题】:Pandas - single positional indexer is out-of-boundsPandas - 单个位置索引器超出范围
【发布时间】:2022-07-06 15:21:52
【问题描述】:

我正在尝试在 python 中运行自动化 sql 脚本,并且我想导出 sql 查询的结果。我使用了这样的代码:

'''table1.loc[table1['colours'] == 'blue' ,'count'].iloc[0]'''

基本上,table1 是一个 2*2 表,其中包含颜色和计数,这里我想要蓝色的计数。查询抛出错误“单个位置索引器超出范围”,我看到原因是因为 table1 中不存在蓝色。但是这个脚本将运行多次,我们有时可能会或可能不会有蓝色。任何人都可以在这里帮助我提供某种案例陈述吗?

【问题讨论】:

    标签: python sql pandas dataframe automation


    【解决方案1】:

    希望这是你想要的:

    df = pd.DataFrame({'color':['blue', 'orange'],
                       'count':[5,6]})
    
    
    
    color  count
    0    blue      5
    1  orange      6
    
    print(df[df.color=='blue']['count'].iloc[0])
    

    5

    【讨论】:

      猜你喜欢
      • 2020-05-29
      • 2019-04-13
      • 2018-03-03
      • 2018-06-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-24
      • 1970-01-01
      相关资源
      最近更新 更多