Daidalos

À-propos


Daidalos

Follow

Contact-me

Log-in to send a nessage

How to calculate the mean over a dataframe column with pandas in python ?

Article
18 juin 2020

Creative Commons License

Abstract: Examples of how to calculate the mean over a dataframe column with pandas in python: [TOC] ### 1 -- Create a dataframe Lets consider the following dataframe: import pandas as pd data = {'Name':['Ben','Anna','Zoe','Tom','John','Steve'], 'Age':[20,27,43,30,12,21]} df = pd.DataFrame(data) returns Name Age 0 Ben 20 1 Anna 27 2 Zoe 43 3 Tom 30 4 John 12 5 Steve 21 ### 2 -- Calculate ...

Comment calculer la moyenne sur une colonne d'une dataframe avec pandas en python ?

Article
18 juin 2020

Creative Commons License

Abstract: Exemples de comment calculer la moyenne sur une colonne d'une dataframe avec pandas en python: [TOC] ### 1 -- Créer une dataframe Soit par exemple la dataframe suivante: import pandas as pd data = {'Name':['Ben','Anna','Zoe','Tom','John','Steve'], 'Age':[20,27,43,30,12,21]} df = pd.DataFrame(data) donne Name Age 0 Ben 20 1 Anna 27 2 Zoe 43 3 Tom 30 4 John 12 5 Steve 21 ### 2 -- ...

How to replace NaN values in a pandas dataframe ?

Article
17 juin 2020

Creative Commons License

Abstract: Examples of how to replace NaN values in a pandas dataframe [TOC] ### 1 -- Create a dataframe Lets consider the following dataframe: import pandas as pd import numpy as np data = {'Name':['Ben','Anna','Zoe','Tom','John','Steve'], 'Age':[20,27,43,30,np.nan,np.nan], 'Gender':['M',np.nan,'F','M','M','M']} df = pd.DataFrame(data) returns Name Age Gender 0 Ben 20.0 M 1 Anna 27.0 NaN 2 Zoe ...

Comment remplacer les NANs dans une dataframe avec pandas en python ?

Article
17 juin 2020

Creative Commons License

Abstract: Exemples de comment remplacer les NANs dans une dataframe avec pandas en python: [TOC] ### 1 -- Créer une dataframe avec pandas Soit par exemple la dataframe suivante: import pandas as pd import numpy as np data = {'Name':['Ben','Anna','Zoe','Tom','John','Steve'], 'Age':[20,27,43,30,np.nan,np.nan], 'Gender':['M',np.nan,'F','M','M','M']} df = pd.DataFrame(data) qui donne Name Age Gender 0 Ben 20.0 M ...

How to convert a dataframe column of date of birth DOB to column of age with pandas in python ?

Article
17 juin 2020

Creative Commons License

Abstract: Examples of how to convert a dataframe column of date of birth DOB to column of age with pandas in python: [TOC] ### 1 -- Create a dataframe Lets consider the following dataframe for example import pandas as pd data = {'Name':['Ben','Anna','Zoe','Tom','John','Steve'], 'dob':['1982-07-08 00:00:00', '1987-03-01 00:00:00', '2016-02-12 00:00:00', '2002-08-14 00:00:00', '2011-01-19 00: ...

How to get the age from a date of birth (DOB) in python ?

Article
17 juin 2020

Creative Commons License

Abstract: Examples of how to get the age from a date of birth (DOB) in python: [TOC] ### 1 -- Calculate the age from a DOB (example 1) Lets consider the following date of birth: July 8, 1982: import datetime dob = datetime.date(1982,8,7) to get the age of that person at the present day (June 16, 2020), a solution is to defined a function: def from_dob_to_age(born): today = datetime.date.today() return today.year - born.year - ((today.month, today.day) < ( ...

Comment transformer une colonne de date de naissance en age avec pandas en python ?

Article
17 juin 2020

Creative Commons License

Abstract: Exemple de comment transformer une colonne de date de naissance en age avec pandas en python: [TOC] ### 1 -- Créer une dataframe Soit par exemple la dataframe suivante: import pandas as pd data = {'Name':['Ben','Anna','Zoe','Tom','John','Steve'], 'dob':['1982-07-08 00:00:00', '1987-03-01 00:00:00', '2016-02-12 00:00:00', '2002-08-14 00:00:00', '2011-01-19 00:00:00', ...

Comment calculer l'age à partir d'une date de naissance en python ?

Article
16 juin 2020

Creative Commons License

Abstract: Exemple de comment calculer l'age à partir d'une date de naissance en python: [TOC] ### 1 -- Transformer une date de naissance en age,(exemple 1) Soit la date de naissance suivante 8 Juillet 1982: import datetime dob = datetime.date(1982,8,7) pour obtenir l'age de cette personne a la date d'aujourd'hui (16 juin 2020), on peut faire comme ceci def from_dob_to_age(born): today = datetime.date.today() return today.year - born.year - ((today.month, ...

How to remove duplicates rows in a pandas dataframe in python ?

Article
16 juin 2020

Creative Commons License

Abstract: Examples of how to emove duplicates rows in a pandas dataframe in python: [TOC] ### 1 -- Create a dataframe Lets create first for example the following dataframe import pandas as pd data = {'Name':['Ben','Anna','Anna','Anna','Zoe','Zoe','Tom','John','Steve'], 'Age':[20,27,27,27,43,43,30,12,21], 'Sex':[1,0,0,0,0,0,1,1,1]} df = pd.DataFrame(data) print(df) returns here Name Age Sex 0 Ben 20 1 1 A ...

How to remove dataframe rows where a condition is true with pandas in python ?

Article
16 juin 2020

Creative Commons License

Abstract: Examples of how to drop dataframe rows where a condition is true with pandas in python [TOC] ### 1 -- Create a dataframe Lets consider for example the following dataframe: >>> import pandas as pd >>> data = {'Name':['Ben','Anna','Zow','Tom','John','Steve'], 'Age':[20,27,43,30,12,21], 'Sex':[1,0,0,1,1,1]} >>> df = pd.DataFrame(data) returns here: >>> df Age Name Sex 0 20 Ben 1 1 27 Anna 0 2 43 Zoe 0 3 30 Tom 1 4 12 John ...

Comment supprimer les doublons dans une dataframe avec pandas en python ?

Article
15 juin 2020

Creative Commons License

Abstract: Exemple de comment supprimer les doublons dans une dataframe avec pandas en python: [TOC] ### 1 -- Créer une dataframe Commençons par créer une simple dataframe avec pandas: import pandas as pd data = {'Name':['Ben','Anna','Anna','Anna','Zoe','Zoe','Tom','John','Steve'], 'Age':[20,27,27,27,43,43,30,12,21], 'Sex':[1,0,0,0,0,0,1,1,1]} df = pd.DataFrame(data) print(df) donne Name Age Sex 0 Ben 20 1 1 ...

Comment supprimer les lignes d'une dataframe ou une condition est vérifiée avec pandas en python ?

Article
15 juin 2020

Creative Commons License

Abstract: Exemples de comment supprimer les lignes d'une dataframe ou une condition est vérifiée avec pandas en python: [TOC] ### 1 -- Créer une simple dataframe Commençons par créer une simple dataframe avec pandas: >>> import pandas as pd >>> data = {'Name':['Ben','Anna','Zow','Tom','John','Steve'], 'Age':[20,27,43,30,12,21], 'Sex':[1,0,0,1,1,1]} >>> df = pd.DataFrame(data) ce qui donne: >>> df Age Name Sex 0 20 Ben 1 1 27 Anna 0 2 43 Zoe 0 ...

How to remove (filter) the duplicates in a python list ?

Article
01 juin 2020

Creative Commons License

Abstract: Examples of how remove (filter) the duplicates in a python list: [TOC] ### 1 -- Create a list Let's consider the following list: >>> l = ['a','a','b','c','d','d','d'] the goal here is to remove the duplicates in the list. ### 2 -- Remove the duplicates using a for loop A first solution is to use a for loop, example: >>> lwd = [] >>> for i in l: ... if i not in lwd: lwd.append(i) ... >>> lwd ['a', 'b', 'c', 'd'] ### 2 -- Remove the duplicates using a d ...

How to check if an element is in a list or not in python ?

Article
01 juin 2020

Creative Commons License

Abstract: Examples of how to check if an element is in a list or not in python: [TOC] ### 1 -- Create a liste Lets create for instance the following list: >>> l ['a', 'b', 'c', 'd', 'e', 'f'] ### 2 -- Check if an element is in the list To check if the element 'c' is in the list called here l, a solution is to use the following logical expression: >>> 'c' in l True check now if 'g' is in the list >>> 'g' in l returns here: False Can then be used with if, illustra ...

Comment supprimer les doublons (répétitions) dans une liste en python ?

Article
01 juin 2020

Creative Commons License

Abstract: Exemples de comment supprimer les doublons (répétitions) dans une liste en python: [TOC] ### 1 -- Creer une simple liste en python Soit la liste suivante par exemple >>> l = ['a','a','b','c','d','d','d'] on veut a supprimer les doublons present dans cette liste. ### 2 -- Utiliser une simple boucle for Une solution est d'itérer sur l'ensemble des éléments de la liste: >>> lwd = [] >>> for i in l: ... if i not in lwd: lwd.append(i) ... >>> lwd ['a', 'b', 'c ...

Timeline Post
27 mai 2020

Making History: NASA and SpaceX Launch Astronauts to Space!


How to get a list of names (variables) of data stored in a HDF5 file using pandas in python ?

Article
27 mai 2020

Creative Commons License

Abstract: Example of how to get a list of names (variables) of data stored in a HDF5 file using pandas in python [TOC] ### 1 -- Read the file First, to read an HDF5 file using pandas, we can do: store = pd.HDFStore('data.hdf5') or with pd.HDFStore('data.hdf5') as store: ... ... ### 2-- Get a list of names (variables) of data stored in a HDF5 file using pandas To get the name of all data stored in the hdf5 file, a solution is to use keys() : sto ...

How to find in a list the elements starting by *** in python ?

Article
26 mai 2020

Creative Commons License

Abstract: Example of how to find in a list the elements starting by *** in python: [TOC] ### 1 - - Create a simple list in python Lets create a liste with words: l = ['name', 'address_01', 'address_02', 'address_03', 'job', 'income'] ### 2 - - Select words that starts by *** For example, lets select only the words that start by 'address' using a list comprehension and the python method [startswith()](https://www.tutorialspoint.com/python/string_startswith.htm): sub_l = [i for ...

How to find the smallest positive value in a list in python

Article
26 mai 2020

Creative Commons License

Abstract: Examples of how to find the smallest positive value in a list in python ? [TOC] ### 1 -- Find the minimum value Let's consider the following list l = [ 7, 3, 6, 9, 2, -1, -5, -4, -3] to find the smallest value, a solution is to use the function min(): min(l) which returns here: -1 ### 2 -- Find the smallest positive value Now to find the smallest positive value a solution is to use a list comprehension and then min(): min([i for i in l if i > 0]) ...

How to find the difference in minutes between two dates in python ?

Article
26 mai 2020

Creative Commons License

Abstract: Examples of how to find the difference in minutes between two dates in python: [TOC] ### 1 -- Create two dates Let's create two dates objects in python using the module datetime: import datetime year = 2008 month = 7 day = 8 hour = 12 minute = 47 second = 0 time1 = datetime.datetime(year,month,day,hour,minute,second) hour = 14 minute = 20 time2 = datetime.datetime(year,month,day,hour,minute,second) ### 2 -- Example 1 u ...

How to select randomly (sample) the rows of a dataframe using pandas in python ?

Article
24 mai 2020

Creative Commons License

Abstract: Example of how to select randomly (sample) the rows of a dataframe using pandas in python: [TOC] ### 1 -- Create a simple dataframe Créons une simple dataframe avec 5 colonnes et 20 lignes: >>> import pandas as pd >>> import numpy as np >>> data = np.arange(1,101) >>> data = data.reshape(20,5) >>> df = pd.DataFrame(data=data,columns=['a','b','c','d','e']) >>> df a b c d e 0 1 2 3 4 5 1 6 7 8 9 10 2 11 12 13 14 15 3 16 ...

How to edit a pandas dataframe column values where a condition is verified in python ?

Article
24 mai 2020

Creative Commons License

Abstract: Examples of how to edit a pandas dataframe column values where a condition is verified in python: [TOC] ### 1 -- Create a simple dataframe with pandas Lets' start by creating a simple dataframe with 5 columns and 20 rows: >>> import pandas as pd >>> import numpy as np >>> data = np.arange(1,101) >>> data = data.reshape(20,5) >>> df = pd.DataFrame(data=data,columns=['a','b','c','d','e']) returns >>> df a b c d e 0 1 2 3 4 5 1 6 7 8 ...

How to select dataframe rows using a condition with pandas in python ?

Article
24 mai 2020

Creative Commons License

Abstract: Examples of how to select a dataframe rows using a condition with pandas in python: [TOC] ### 1 -- Create a simple dataframe Lets start by creating a simple dataframe with pandas: >>> import pandas as pd >>> data = {'Name':['Ben','Anna','Zow','Tom','John','Steve'], 'Age':[20,27,43,30,12,21], 'Sex':[1,0,0,1,1,1]} >>> df = pd.DataFrame(data) returns >>> df Age Name Sex 0 20 Ben 1 1 27 Anna 0 2 43 Zow 0 3 30 Tom 1 4 12 Joh ...

How to select dataframe columns that start with *** using pandas in python ?

Article
24 mai 2020

Creative Commons License

Abstract: Examples of how to select dataframe columns that start with *** using pandas in python: [TOC] ### 1 -- Create a dataframe Lets start by creating a simple dataframe with 8 columns: import pandas as pd import numpy as np data = np.arange(1,33) data = data.reshape(4,8) df = pd.DataFrame(data=data,columns=['name','add_01','add_02','add_03', 'counrty','streed','zip code','county']) print(df) returns name ...

Comment obtenir une liste des noms des données stockées dans un fichier hdf5 avec pandas en python ?

Article
19 mai 2020

Creative Commons License

Abstract: Exemple de comment obtenir une liste des noms des données stockées dans un fichier hdf5 avec pandas en python: [TOC] ### 1 -- Lire le fichier Pour lire le fichier hdf (par exemple 'data.hdf5') avec pandas on peut faire comme ceci: store = pd.HDFStore('data.hdf5') ou with pd.HDFStore('data.hdf5') as store: ... ... ### 2-- Obtenir une liste des noms des données stockées dans un fichier hdf5 Pour alors obtenir une liste des noms des données st ...

Chercher