How to install tensorflow on a mac studio ?


Recently bought a new mac studio and needed Tensorflow for my work. On my previous iMac I installed tensorflow using Anaconda and it worked very well. However now with my new mac studio (apple M1 chip), I got the following error on a jupyter noteboook: "the kernel appears to have died. it will restart automatically" when I do: import tensorflow as tf And if I launch python on the terminal: import tensorflow as tf gives ERROR: Could not find a version that sat ...


by Benjamin
Created Dec. 3, 2022, 1:20 a.m.
Tags Python; TensorFlow; Mac;

How to check if a pandas dataframe is empty in python ?


Examples of how to check if a pandas DataFrame is empty: [TOC] ### Create an empty dataframe Let's create an empty dataframe with pandas import pandas as pd df = pd.DataFrame() print(df) gives Empty DataFrame Columns: [] Index: [] ### Check if dataframe is empty To check if a dataframe is empty a solution is to use "empty": df.empty returns True here. With a if statement: if df.empty: print("Oups, your ...


by Benjamin
Created Dec. 3, 2022, 12:31 a.m.
Tags Python; Pandas; Dataframe;

How to pass optional arguments in a python function ?


Examples of how to pass optional arguments in a python function? [TOC] ### Basic python function Let's start by creating a simple function in python: def function_test(a,b): print(a,b) return None myfunc(1,2) gives 1 2 However if we try to pass a third variable myfunc(1,2,7) will generate an error message too many values to unpack (expected 2) def myfunc(a,b): print('function print', a,b) return a ...


by Benjamin
Created Nov. 29, 2022, 10:14 p.m.
Tags Python; Function;
Language(s): fr

How to iterate over the first n rows of a pandas DataFrame in python ?


Examples of how to iterate over the first n rows of a pandas DataFrame in python [TOC] ### Create a pandas dataframe Let's first create a DataFrame: import pandas as pd import numpy as np data = np.arange(1,31) data = data.reshape(10,3) df = pd.DataFrame(data, columns=['A','B','C']) print(df) gives A B C 0 1 2 3 1 4 5 6 2 7 8 9 3 10 11 12 4 13 14 15 5 16 17 18 6 19 20 ...


by Benjamin
Created Nov. 29, 2022, 9:43 p.m.
Tags Python; Pandas; Dataframe;
Language(s): fr

How to create a pandas dataframe from a python dictionary ?


Examples of how to create a pandas dataframe from a python dictionary: [TOC] ### Create a dataframe from a python dictionary (method 1) To create a dataframe from a python dictionary: d = { 'Name': ['Ben', 'John', 'Emma', 'Zoe'], 'Age': [40, 56, 34, 12] } a solution is to do import pandas as pd df = pd.DataFrame(d) gives Name Age 0 Ben 40 1 John 56 2 Emma 34 3 Zoe 12 ### Create a dataframe ...


by Benjamin
Created Nov. 27, 2022, 3:29 a.m.
Tags Python; Pandas; Dataframe; Dictionary;
Language(s): fr

How to plot a rectangle on a cartopy map with python ?


Examples of how to plot a rectangle on a cartopy map with python: [TOC] ### Example 2 (PlateCarree projection) from matplotlib.pyplot import figure import matplotlib.pyplot as plt import matplotlib.patches as mpatches import cartopy.crs as ccrs fig = figure(num=None, figsize=(12, 10), dpi=100, edgecolor='k') proj = ccrs.PlateCarree() ax = plt.axes(projection=proj) ax.set_global() long = -70. lat = -45. ax.add_patch(mpat ...


by Benjamin
Created Nov. 26, 2022, 3:41 p.m.
Tags Python; Cartopy; Map; Rectangle;
Language(s): fr

How to plot a circle on a cartopy map with python ?


Examples of how to plot a circle on a cartopy map with python: [TOC] ### Plot a red circle using PlateCarree projection To plot a circle on a cartopy map, a solution is to use matplotlib [patches](https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.patches.Circle.html) : from matplotlib.pyplot import figure import numpy as np import matplotlib.pyplot as plt import cartopy.crs as ccrs import matplotlib.patches as mpatches fig = figure(num=None, figsize ...


by Benjamin
Created Nov. 26, 2022, 3:48 a.m.
Tags Python; Cartopy; Circle;
Language(s): fr

How to minify html or css files for improving web site speed using python ?


Examples of how to minify html or css files for improving web site speed using python: [TOC] ### Minify html using htmlmin A solution is to use the python module [htmlmin](https://pypi.org/project/htmlmin/) that can be installed using pip: pip install htmlmin or [conda-forge](https://anaconda.org/conda-forge/htmlmin) conda install -c conda-forge htmlmin Then to minify a html text: import htmlmin html_txt = '''<!DOCTYPE html> <html> <body> ...


by Benjamin
Created Nov. 25, 2022, 3:43 a.m. | Edited Nov. 25, 2022, 7:26 p.m.
Tags Python; HTML; CSS; Minify;
Language(s): fr

How to merge two folders using python ?


Example of how to merge two folders using python [TOC] ### Introduction I used google takeout to download a project that has been saved on google drive. However since the project is big, the project has been divided in two folders during the download: Takeout_01 and Takeout_02 folders. Folders paths in my local computer: takeout_01_path = '/Users/JDoe/Downloads/Takeout_01' takeout_02_path = '/Users/JDoe/Downloads/Takeout_02' The goal was then to ...


by Benjamin
Created Nov. 24, 2022, 4:59 a.m. | Edited Nov. 24, 2022, 3:10 p.m.
Tags Python; Linux;
Language(s): fr

How to switch dictionary keys and values in python ?


Example of how to switch dictionary keys and values in python: [TOC] ### Switching dictionary keys and values Let's consider the following dictionary: d = {'A':1, 'B':2, 'C':3} To iterate over all keys and values we can use [items()](https://docs.python.org/3/tutorial/datastructures.html): for x, y in d.items(): print(x,y) gives A 1 B 2 C 3 Then to create a new dictionary with inverted keys and values a straigtforward solution is to do: ...


by Benjamin
Created Nov. 23, 2022, 2:43 p.m.
Tags Python; Dictionary;
Language(s): fr

Search: