0

I'm trying to conditionally drop rows out of a pandas dataframe, using syntax as such:

if ((df['Column_1'] == 'value_1') & (df['Column_2'] == 'value_2')):
    df['Columns_3'] == df['Column_4']
else:
    df.drop()

Thanks in advance for the help.

Breton
  • 15
  • 1
  • 5

2 Answers2

1

Try something like

df = df.drop(df[(df['Column1'] != 'value_1') & (df['Colum2'] != 'value_2')].index)
df['Column3'] = df['Column4']
Arthur Gouveia
  • 734
  • 4
  • 12
  • This looks right, but can it be expanded across multiple conditions? Say in my post after the if statement and before drop, you have: `elif ((df['Column_1'] == 'value_1') & (df['Column_2'].isin(['value_3', 'value_4', 'value_5']))).all(): df['Column_3'] == df['Column_5']` – Breton Sep 30 '17 at 13:03
0
df.loc[(df['Column_1'] == 'value_1') & (df['Column_2'] == 'value_2'),'Column3'] = df.loc[(df['Column_1'] == 'value_1') & (df['Column_2'] == 'value_2'),'Column4']

df = df[df['Column3]==df['Column4']

When working with Pandas, best to get into the practice of trying to use some of the built-in C loop functionality.

There would also be a way of updating Column 3 based on the above condition. I'd need a data set to play around with though. Something like...

df['Column3'].update(the_condition)

update requires that the indices line up

could also look into transform and apply

df['Column3'].transform(the_condition)

or:

df['Column3'].apply(the_condition)
Yale Newman
  • 1,141
  • 1
  • 13
  • 22