python-标签不在[index]中

时间:2018-10-05 21:59:03

标签: python pandas indexing

我正在使用for循环创建一个函数,该函数通过查看每行中的方向值来根据条件返回方向。 我将整个数据集一分为二。 我正在使用:

def dir_func(df):
for i in df.index:
    if df.loc[i,'dir_1'] == ['E' or 'NE' or 'SE']:
        df.loc[i,'dir_fin'] = 'E'
    elif df.loc[i,'dir_1'] == ['W' or 'NW' or 'SW']:
        df.loc[i,'dir_fin'] = 'W'
    else:
        if df.loc[i,'dir_2'] == ['E' or 'NE' or 'SE']:
            df.loc[i,'dir_fin'] = 'E' 
        elif df.loc[i,'dir_2'] == ['W' or 'NW' or 'SW']:
            df.loc[i,'dir_fin'] = 'W'
        else:
            if (pd.Series(df.loc[i,'loc_01']).str.contains(
                ("\sEB\s" or "\sEast\s" or "\sEastbound" or "\sE\s"),case=False) \
            | pd.Series(speed_overspeed.loc[i,'loc_02']).str.contains(
                ("\sEB\s" or "\sEast\s" or "\sEastbound" or "\sE\s"),case=False)).bool()==True:
                df.loc[i,'dir_fin'] = 'E' 
            elif (pd.Series(df.loc[i,'loc_01']).str.contains(
                ("\sWB\s" or "\sWest\s" or "\sWestbound" or "\sW\s"),case=False) \
            | pd.Series(speed_overspeed.loc[i,'loc_02']).str.contains(
                ("\sWB\s" or "\sWest\s" or "\sWestbound" or "\sW\s"),case=False)).bool()==True:
                df.loc[i,'dir_fin'] = 'W' 
            else:
                df.loc[i,'dir_fin'] = 'NA'

进行迭代。

此函数对我的第一个拆分数据集效果很好,但是当我尝试将其应用于第二个数据集时,它将返回:

  

KeyError:“标签[891]不在[索引]中”

但是,当我查看#891值时,它实际上会返回包含值的行。

我想知道是否有人可以帮助我解决这个问题,我将不胜感激。

非常感谢您!

1 个答案:

答案 0 :(得分:0)

我认为您应该了解(在python 3.x中)

的结果
PHP
除非df.loc [i,'dir_1'] 实际上等于'E'

,否则

始终将是std::vector<std::string> GetDeck() { // array of card rank and vlaue, seperated by a period, used as a delimiter for evaluting the players hands std::vector<std::string> Deck = { "A.H","2.H","3.H","4.H","5.H","6.H","7.H","8.H","9.H","10.H","J.H","Q.H","K.H", "A.S","2.S","3.S","4.S","5.S","6.S","7.S","8.S","9.S","10.S","J.S","Q.S","K.S", "A.D","2.D","3.D","4.D","5.D","6.D","7.D","8.D","9.D","10.D","J.D","Q.D","K.D", "A.C","2.C","3.C","4.C","5.C","6.C","7.C","8.C","9.C","10.C","J.C","Q.C","K.C", }; return Deck; } int main() { auto Deck = GetDeck(); std::cout << Deck[23] << '\n'; }

因此,df.loc[i,'dir_1'] == ('E' or 'NE' or 'SE') 语句应更改为

False

类似地,您可以在最后一个比较语句中使用if

最终功能将是:

 if df.loc[i,'dir_1'] in ['E', 'NE', 'SE']: