Posts

SQL Number Format

  Here’s an example: SELECT FORMAT(1, 'N'); Result: 1.00 In this case, I used  N  as the second argument. This is the  standard numeric format specifier  for Number. This particular format specifier ( N ) results in the output being formatted with integral and decimal digits, group separators, and a decimal separator with optional negative sign. This argument is case-insensitive, so either  N  or  n  is fine. Decimal Places Here’s another example, this time using  N1  to specify that we only want one decimal place: SELECT FORMAT(1, 'N1'); Result: 1.0 But you can also increase the number of decimal places too: SELECT FORMAT(1, 'N7'); Result: 1.0000000

Drop table if exists in SQL

Create temp table in SQL Server   drop table if exists #sfa_measurement into #sfa_measurement

Date_range in pandas

  Creating a   DataFrame   by passing a NumPy array, with a datetime index and labeled columns: In [5]: dates = pd . date_range ( "20130101" , periods = 6 ) In [6]: dates Out[6]: DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'], dtype='datetime64[ns]', freq='D') In [7]: df = pd . DataFrame ( np . random . randn ( 6 , 4 ), index = dates , columns = list ( "ABCD" )) In [8]: df Out[8]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 2013-01-06 -0.673690 0.113648 -1.478427 0.524988

Read Parquet file in pandas

callplan_202205 = pd.read_parquet('../../ReportMart/Call_Plan/data/call_plan_202205.parquet', engine='pyarrow')

Case When in Numpy

 callplan['CH'] = np.where(callplan['CH'] == 'ER', 'Modern Off & Digital',                 np.where(callplan['CH'] == 'MF', 'Modern Off & Digital',                 np.where(callplan['CH'] == 'UF', 'Urban Off',                 np.where(callplan['CH'] == 'UN', 'Traditional On',                 np.where(callplan['CH'] == 'MN', 'Modern On',                 np.where(callplan['CH'] == 'RM', 'Rural On & Off',                 callplan['CH']                                               ))))))

WHERE or IF function in Numpy

Script union['CH'] = np.where(union['Invoice Type'] == 'Normal Sales', union['Channel Name'],'Wedding & Function') Explanation IF "Invoice Type" = "Normal Sales" then union['CH'] = union['Channel Name'] Else union['CH'] = "Wedding & Function"