Skip to content

Instantly share code, notes, and snippets.

@godber
Last active May 16, 2016 09:50
Show Gist options
  • Select an option

  • Save godber/fd2e9793d984db553ca066a99462f7ea to your computer and use it in GitHub Desktop.

Select an option

Save godber/fd2e9793d984db553ca066a99462f7ea to your computer and use it in GitHub Desktop.
Pandas Operate on Multiple Rows with Boolean Operator
import pandas as pd
def r(row):
if row['lat'] < 5:
return row['data']
elif row['lat'] >= 5:
return 10 * row['data']
df = pd.DataFrame({ 'data': range(10), 'lat': range(10)}, columns=["lat", "data"])
print(df)
df['adj'] = df.apply(r, axis=1)
print(df)
@mdengler
Copy link

How about:

df['adj'] = df['data']
rows_to_scale = df['lat'] >= 5
df.loc[rows_to_scale, 'adj'] *= 10.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment