Frank White
Frank White

Reputation: 321

drop pandas dataframe in a loop based on 2 conditions

I want to drop every dataframe that fullfils the condition in the if clause:

for i in range(0,len(df)):
    if((df.iloc[i][5]=='District Steam') & (df.iloc[i][1]!='Baltimore')):
        df.drop(df.iloc[i])

Whenever the 5. value is District Steam, I only want to keep the rows in which the city is Baltimore It provides the following error:

KeyError: "['Office' 'Miami' 'District Steam'] not found in axis"

Upvotes: 0

Views: 48

Answers (1)

ansev
ansev

Reputation: 30930

I think you want:

df = df.loc[df.iloc[:, 5].ne('District Steam') | df.iloc[:, 1].eq('Baltimore')]

In other case please show an example and your expected output

Upvotes: 1

Related Questions