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.
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.
Try something like
df = df.drop(df[(df['Column1'] != 'value_1') & (df['Colum2'] != 'value_2')].index)
df['Column3'] = df['Column4']
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)