date difference: example 1: def dd(date1,date2): d1 = datetime.strptime(date1,’%m/%d/%y’) d2 = datetime.strptime(date2,’%m/%d/%y’) diffDays = (d1-d2).days return diffDays example 2: from datetime import datetime, timedelta ini_time_for_now = datetime.now() future_date_after_2yrs = ini_time_for_now + timedelta(days = 730) future_date_after_2days = ini_time_for_now + timedelta(days = 2) past_date_before_2yrs = ini_time_for_now – timedelta(days = 730) past_date_before_2hours = ini_time_for_now – timedelta(hours = 2) ..
Tag : Python
Rename columns x1 to x3, x2 to x4 from pyspark.sql import SparkSession spark=SparkSession.builder.appName(‘rename columns’).getOrCreate() data = spark.createDataFrame([(1,2), (3,4)], [‘x1’, ‘x2’]) data.show() data = data.withColumnRenamed(‘x1′,’x3’) \ .withColumnRenamed(‘x2’, ‘x4’) d..
import cv2 input_imgfn=”tobrighten.jpg” output_imgfn=”brightened.jpg” def change_brightness(img, value=30): hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) h, s, v = cv2.split(hsv) v = cv2.add(v,value) v[v > 255] = 255 v[v < 0] = 0 final_hsv = cv2.merge((h, s, v)) img = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR) return img img = cv2.imread(input_imgfn) #load rgb image img = change_brightness(img, value=90) #increases #img = change_brightness(img, value=-30) ..
rename columns name: df = df.rename(columns={“oldcol1″:”newcol1″,”oldcol2”: “newcol2”}) change value of a column under a condition: df_confirmed.loc[df_confirmed[‘country’] == “US”, “country”] = “USA” replace NaN with some value: df_confirmed = df_confirmed.replace(np.nan, ”, regex=True) drop several columns(Lat and Long): df = df.drop([‘Lat’,’Long’..
A good example for subplot second y-axis: from numpy.random import rand import matplotlib import matplotlib.pyplot as plt ax0 = plt.subplot(211) ax1 = ax0.twinx() ax0.plot(rand(1) * rand(10),’r’) ax1.plot(10*rand(1) * rand(10),’b’) plt.show() based on the..
Based on my code: Canada_COVID19_cases_information.ipynb I like this way to convert string to date: from pyspark.sql.types import * #data types func = udf (lambda x: datetime.strptime(x, ‘%d/%m/%Y’), DateType()) df = df.withColumn(‘newDate’, func(col(‘Date’))) calculate difference days between two date: Some good examples from pyspark.sql import functions as F df = df.withColumn(‘startDay’,F.lit(‘2020-01-01’).cast(“Date”)) df = df.withColumn(‘Days_from_01_Jan’,F.datediff(F.col(‘newDate’),F.col(‘startDay’))) convert pandas ..