Skip to content

Instantly share code, notes, and snippets.

@datagy
Created October 21, 2022 11:19
Show Gist options
  • Select an option

  • Save datagy/f1703c61d47b2b923ee172e05d99f178 to your computer and use it in GitHub Desktop.

Select an option

Save datagy/f1703c61d47b2b923ee172e05d99f178 to your computer and use it in GitHub Desktop.
Using an IF Statement in Pandas
# Using if Statements in Python
import pandas as pd
df = pd.DataFrame({
'Name': ['Nik', 'Kate', 'Evan'],
'Location': ['Toronto, ON', 'Atlanta, GA', 'Portland, OR'],
'Total': [99.99, 125.65, 33.43]
})
df['Over 100'] = ['Yes' if Total > 100 else 'No' for Total in df['Total']]
print(df)
# Returns:
# Name Location Total Over 100
# 0 Nik Toronto, ON 99.99 No
# 1 Kate Atlanta, GA 125.65 Yes
# 2 Evan Portland, OR 33.43 No
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment