我有一个嵌套循环。在外循环中,我遍历比内循环中的数据帧[键]更多的键。这会导致 IndexError (key cannot be found in dataframe[key]。我需要一种方法来检查键是否匹配。
fe 只是关键列:
dfKeys dataframe
------- --------
key key
1 1
1 3
3 5
3 9
3
4
4
5
5
5
5
8
8
9
9
9
grouped=dfKeys.groupby('key')
for key, group in grouped:
if ((group.someCol=='someVal').any() or ~(group.someCol.isin(someArray).any())):
if(key in dataframe['key']): #######this did not solve it, always false (but there have to be some matches)
foundIndex=dataframe[dataframe['key']==key].index.values.astype(int)[0] #INDEXERROR after some loops if there is no 'if' above
dataframe.loc[foundIndex,'myCol']='myVal'
类型dataframe['key']是熊猫系列。