Here is my now goto prompt for when I am working on a project – have a class that I need to mimic the interfaces. use the following class as a template. Call the new class “MainframeExcelText” The init take file and sheet and sets row and column location to 0. 1 function – write_text will take a row,col and […]
Bing Chat – use a class as a template prompt
Pandas – convert from dataframe append to concat.
the latest Pandas gives warning about append being depreciated. FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead. The main difference is that concat expects an iterable list. monnth_df = pd.concat( [ month_df,obs1 ], ignore_index=True) # month_df = month_df.append(obs1, ignore_index=True)
Python Pandas – how to copy dtypes to a new dataframe copy.
obs1 is the source dataframe “astype” actually copies the dataframe with type data and column data new_data = obs1.astype(obs1.dtypes.to_dict()) new_data = new_date[0:1,:]
Python – how to construct SQL with “AND” and “OR” with LIKE
def run_query_table_multi( search, and_flag=False ): if and_flag is True: combo = ” and ” else: combo = ” or ” try: conn = mariadb.connect( user=self.user, password=self.password, host=self.host, port=self.port, database=self.database ) except mariadb.Error as e: print(f”Error connecting to MariaDB Platform: {e}”, e) sys.exit(1) # Get Cursor cur = conn.cursor() my_sql_qry = “SELECT rec_id, title, http from recipe where” q_bld = “” […]
Python3 – strip html tags from string
Here is a code snippet to strip tags and leading and trailing whitespace def remove_html_tags text): “””Remove html tags from a string””” clean = re.compile(‘<.*?>’) ntxt = re.sub(clean, ”, text ) return ntxt.strip()
Numpy – array of strings.
Numpy supports strings and array of strings. It is fixed length. If you replace an array element with a longer string it truncates it to fit. Each python character is 4 bytes ( unicode ). So a 40 character element in the array is 160 bytes. See example from Notebook below.
Pandas – how to edit columns for html table
How to edit table so that index and column names are on the same row. import beatifulsoup. from bs4 import BeautifulSoup find the empty tags in ‘thead’ and then fill with col names from the dataframe. Note: If you code “header=False”, the default style for table row will be gone <tr style=”text-align: right;”> html_doc = tide_table.to_html(header=False) soup = BeautifulSoup(html_doc, ‘html.parser’) […]
Pandas/Python – how to drop a row with ‘NaN’
So, you want to get rid of any row that has ‘NaN’ ( null or not a number ) values, because it doesn’t work with some functions ( or can’t ignore ) obs1.dropna(how = ‘all’, subset = [‘wind_mph’], inplace = True) obs1 = obs1.reset_index(drop=True) Another important point – is that with Pandas – you have to convert any value you […]
Windows – how to start Jupyter notebook without anaconda
So, you pip installed Jupyter. Thinking – hey don’t have to start up Anaconda environment to run it. C:\Users\jonallen\Documents\github\weather_obs>pip install jupyter Collecting jupyter Downloading jupyter-1.0.0-py2.py3-none-any.whl (2.7 kB) Collecting ipywidgets Downloading ipywidgets-7.6.3-py2.py3-none-any.whl (121 kB) |████████████████████████████████| 121 kB 3.3 MB/s Collecting notebook Downloading notebook-6.4.3-py3-none-any.whl (9.9 MB) |████████████████████████████████| 9.9 MB 234 kB/s Collecting jupyter-console Downloading jupyter_console-6.4.0-py3-none-any.whl (22 kB) Collecting nbconvert Downloading nbconvert-6.1.0-py3-none-any.whl (551 […]
Using Pandas append on groups of CSV files
This function loads a series of daily csv files and combines them into a monthly dataframe Create an empty Pandas DataFrame append to empty DataFrame after each load. Word of caution – the columns must be the same for all inputs. One should test the columns to make sure you get what you expect. def load_monthly_noaa_csv_files( dir, noaa_station, ext = […]