如果我有一个空表,请调整变量

问题描述 投票:0回答:1

我有一个过滤的数据框:

börse2 = bör[(bör['Ausprägung'] == bor)].iloc[0:1, : ]

在某些情况下看起来像这样(空):

enter image description here

当我运行命令时:

bor2 = börse2.iloc[0]['Schlüssel']

我收到错误:

IndexError:单个位置索引器超出范围

这很有意义,在这种情况下,我如何看到bor2= 111?是否有其他选项代替try except。尝试除外的问题是,还会出现另一个错误,但是我想仅在我的数据帧bor2为空时才明确使用它。

python pandas
1个回答
2
投票

因此,如果使用boolean indexing

boolean indexing

并且必须测试空白数据框使用börse2 = bör[(bör['Ausprägung'] == bor)]

DataFrame.empty

但是如果要匹配列DataFrame.empty的某个第一个值,如果匹配,则为一些默认值,这里if börse2.empty: print ('no match') else: print ('match') 使用colno match技巧:

next
© www.soinside.com 2019 - 2024. All rights reserved.