Useful Python pandas codes

Categories

To Rename the data frame
df.rename(columns={“contract_id”:”deal_id”},inplace=True)

Where statement
tips[tips[‘time’] == ‘Dinner’].head(۵)

vlookup
mg = pd.merge(df,AgReg,on=”deal_id”,how=”left”)

choose the first column of an array or first part of a string with a delimitter
df[“cat”] = df[“CategoryID”].str.split(‘,’,1).str[0]

filling na or nan or Null values
df[“CategoryID”].fillna(“”,inplace=True)

Convert To date time
pd.to_datetime(df[“start_date”],errors=’ignore’)

combination of where and select some.
df[(df[‘CategoryName_Art’] == 1)].iloc[0:5,:]

Get data ready for decision tree dmdf = pd.get_dummies(df[[‘ShamsiYear’, ‘ShamsiYearMonth’, ‘Brand’,       ‘CategoryName’, ‘SubCategoryName’,  ‘UnitPrice’, ‘SoldCoupon’,       ‘Area2’]])