pandas cheat sheet

Apply Weighing to Values

df["Status"] = df["Status"].astype("category")
df["Status"].cat.set_categories(["won","pending","presented","declined"],inplace=True)

Apply a function to a column

# create a function called times100
def times100(x):
    # that, if x is a string,
    if type(x) is str:
        # just returns it untouched
        return x
    # but, if not, return it multiplied by 100
    elif x:
        return 100 * x
    # and leave everything else
    else:
        return

df['name'].map(times100)

Try and Fix datetime

from dateutil import parser
import datetime
def str_to_dt2(s):
    try:
        date_object = parser.parse(str(s))
    except ValueError:
        date_object = datetime.datetime(1970,1,1)
    except:
        date_object = datetime.datetime(1970,1,1)
    return date_object

v2['ts']=v2.ID_EXPIRY.apply(str_to_dt2)

Capitalize

This changes the Column in-place

def capitalizer(x):
   return x.upper()
v2.Telco.apply(capitalizer)

Delete rows based on a column value

df = df[df.line_race != 0]

Strip ALL Spaces and 0's from string

people.Id = people.Id.str.strip().str.lower().str.replace(' ', '').str.replace('0','')

Strip Leading 0 and leading 0 from a String

people.Id = people.Id.str.strip().str.lower().str.lstrip(' ').str.lstrip('0')