Skip to content

Instantly share code, notes, and snippets.

@dtrugman
Created June 27, 2023 22:25
Show Gist options
  • Save dtrugman/09fb86901b18cd4ef674eaf6cf58af67 to your computer and use it in GitHub Desktop.
Save dtrugman/09fb86901b18cd4ef674eaf6cf58af67 to your computer and use it in GitHub Desktop.
Pandas cheat sheet
import pandas as pd
# Create new DataFrame
data = {
"FirstName" : ["Daniel", "Jenny", "Mia"],
"LastName" : ["Trugman", "Altman", "Herman"],
"Age" : [35, 36, 28]
}
df = pd.DataFrame(data, index = [1, 2, 3])
df.head()
# Select one column
df['FirstName']
# Select multiple columns
df[['FirstName', 'LastName']]
# Select one row
df.loc[2]
# Select multipe rows
df.loc[2:3]
# Select specific columns from multiple rows
df.loc[2:3, 'Age']
# Select all the rows where a given column fullfils a condition
cond = df['Age'] > 30
df_age = df.loc[cond] # -OR- df[cond]
# Run method on all values in a column
df['Age'] = df['Age'] + 1
# Conditional set values in column
# Column to set = (value if condition true).where(condition, value if condition false)
df['FirstName'] = ('Old ' + df['FirstName']).where(df['Age'] > 30, df['FirstName'])
df['LastName'] = ('Young ' + df['LastName']).where(df['Age'] < 30, df['LastName'])
# Remove column
df = df.drop('Age', axis=1)
# Create new column with constant value
df['NewAge'] = 30
# Create/Update column with values
df['NewAge'] = [30,20,18]
df.loc[len(df) + 1] = ['Shawn', 'Cohen', 11]
df.loc[len(df) + 1] = {'NewAge': 76, 'FirstName': 'Boris', 'LastName': 'Johnson'}
# Sort values
df = df.sort_values('NewAge', ascending=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment