Mar 7, 2024

install and use Jupyter notebook for Python

In cmd window,

To install,

        python -m pip install notebook


To use,

        jupyter notebook



Nov 15, 2023

make a Python code executable (.exe)

Method 1.

python -m pip install pyinstaller

python -m PyInstaller --noconfirm --onefile --windowed  code.py



Method 2.

python -m pip install auto-py-to-exe

python -m auto_py_to_exe



adjust column lengths when using Pandas ExcelWriter

 To dynamically adjust all the column lengths,

        writer = pd.ExcelWriter('temp.xlsx') 

        df.to_excel(writer, sheet_name='sheetName', index=False)

        for column in df:

                column_length = max(df[column].astype(str).map(len).max(), len(column))

                col_idx = df.columns.get_loc(column)

                writer.sheets['sheetName'].set_column(col_idx, col_idx, column_length)

        writer.close()



To manually adjust a column using column name,

        col_idx = df.columns.get_loc('columnName')

        writer.sheets['sheetName'].set_column(col_idx, col_idx, 20)



To manually adjust a column using column index,

        writer.sheets['sheetName'].set_column(col_idx, col_idx, 20)



Potential error messages:

        AttributeError: 'Worksheet' object has no attribute 'set_column'

-->  install "xlsxwriter" module

-->  use the installed as the engine,  writer = pd.ExcelWriter('temp.xlsx', engine='xlsxwriter') 


remove "FutureWarning" messages in Python programs

We can remove "FutureWarning" messages in Python programs by adding:

        import warnings 

        warnings.simplefilter(action='ignore', category=FutureWarning)


Nov 9, 2023

[Windows] to get the CD-key of current Windows

 To get the CD-key of current Windows,

1.  run  PowerShell or cmd 

2.  execute either 

    wmic path softwareLicensingService get OA3xOriginalProductKey

or

    powershell "(Get-WmiObject -query 'select * from SoftwareLicensingService').OA3xOriginalProductKey"



Oct 5, 2022

[Python] join two dataframe according to specific column

 To join two dataframe (df1, df2) according to specific column (e.g., X,Y,Z),


# merge two dataframe comparing X column data of df1 and Y column data of df2

# merge rows where X value on df1 is same to Y value of df2  (other rows will be abandoned)

# in the merged dataframe, "_x" will be added to column label of df1 and "_y" will be added to column label of df2

          pd.merge(df1, df2, left_on="X", right_on="Y")     


# comparing multiple columns

# rows with same X, Y, Z values on df1 and df2 will merge  (other rows will be abandoned)

# in the merged dataframe, "_x" will be added to column label of df1 and "_y" will be added to column label of df2 

          pd.merge(df1, df2, left_on=['X ','Y','Z'], right_on=['X ','Y','Z'])


Sep 28, 2022

[Python] to compare/calculate Pandas columns

to compare Pandas columns, 
    print(df['Column1'].equals(df['Column2'])) 

if df['Column1'] is equal to df['Column2'], it will return "True" otherwise "False" 


to calculate/assign values,
    print(df['Column1'] + df['Column2']) 

import numpy as np
import pandas as pd

a = [['10', '1.2', '4.2'], ['15', '70', '0.03'], ['8', '5', '0']]
df = pd.DataFrame(a, columns=['one', 'two', 'three'])

df['que'] = np.where((df['one'] >= df['two']) & (df['one'] <= df['three']), df['one'], np.nan)

result will look like:
          one  two three  que
   0     10    1.2   4.2    10
   1      5     70    0.03  NaN
   2      8     5      0       NaN