Estimation du temps de lecture : 5 procès-verbal
Suppression des erreurs indésirables dans vos données, en toute simplicité.
Le processus d'importation de données peut prendre plusieurs formats, mais avez-vous cherché une vidéo sur la manière de procéder ? Mieux encore, vous cherchez une vidéo qui vous montre comment importer un fichier CSV, puis le nettoyer, en supprimant les caractères indésirables ?
Pour faire suite à Python - how do I remove unwanted characters, cette vidéo axée sur le nettoyage des données créées dans le code, cette vidéo passe en revue plusieurs options pour ouvrir un fichier CSV, trouver les caractères indésirables, les supprimer de l'ensemble de données et ensuite renvoyer les données nettoyées.
Comment entrer parmi les données problématiques :
L'approche adoptée ici a consisté à examiner trois scénarios différents :
(A) Utiliser une variable qui est égale à une fonction ouverte, puis lire la variable dans une trame de données.
(B) Utilisation conjointe d'une fonction "avec déclaration" et d'une fonction ouverte, et retour de la sortie dans une trame de données.
(C) Utiliser read_csv pour lire rapidement et efficacement dans le fichier CSV et ensuite nettoyer l'ensemble de données.
Quelques petits accrocs sur la route qui méritent réflexion
Il y a quelques défis à relever dans ce domaine que vous verrez dans la vidéo :
- Les options A et B ont dû déployer un code supplémentaire juste pour obtenir la trame de données comme nous le voulions.
- Les lignes de code supplémentaires rendaient la tâche plus complexe si vous essayiez de la gérer.
En fin de compte, read_csv était probablement la meilleure façon d'aborder ce problème ; il nécessitait moins de code et aurait été plus facile à gérer à long terme.
Comme toujours, merci de nous suivre sur notre chaîne YouTube, et de nous suivre sur les médias sociaux !
Data Analytics Irlande
Le code utilisé dans cette vidéo est ici :
########## How to import a CSV dataset and remove unwanted characters ###########
#Numbers need to be put between "" to be looped through
import pandas as pd
l = ['$','*','€','&','£','/','@']
print("Error list we will be checking against in all three scenarios")
print(l)
############ Scenario 1 - Using the Open Function #####################
#can't use engine ='python' here , as it is an invalid keyword argument for open()
my_file = open("C:/dataanalyticsireland/Python Code/youtube/codeused/files/importandcleandatacsv.csv", "r").read().split('\n')
print("Senario 1 printed as a dataframe")
df = pd.DataFrame(my_file)
df.columns = ['Temp']
df[['Number','Error']] = df['Temp'].str.split(',',expand=True)
#Drop the column Temp ,0 for rows and 1 for columns.
df = df.drop('Temp', 1)
#Dropping the first row as it has the headers in it.
df = df.drop(0)
#Resetting the index to start at value 0
df = df.reset_index(drop=True)
display(df)
def run_scenario1(*l):
#This line extracts all the special characters into a new column
#Using regular expressions it finds values that should not appear
df['Error'] = df['Number'].str.extract('(\D+|^(0)$)')
#This line removes anything with a null value
df.dropna(subset=['Error'], inplace=True)
#This line reads in the list and assigns it a value i, to each element of the list.
#Each i value assigned also assigns an index number to the list value.
#The index value is then used to check whether the value associated with it is in the df['Number'] column
#and then replaces if found
for i in l:
df['Fix']= df['Number'].str.replace(i[0],"").str.replace(i[1],"").str.replace(i[2],"").str.replace(i[3],"") \
.str.replace(i[4],"").str.replace(i[5],"").str.replace(i[6],"")
#print("Error list to check against")
#print(i[0])
#print(i[1])
#print(i[2])
#print(i[3])
#print(i[4])
#print(i[5])
#print(i[6])
print("Scenario 1 output with fixes")
display(df)
run_scenario1(l)
############ Scenario 2 - Using a with statement #####################
#Once Python exits the with block, it closes the file
#can't use engine ='python' here , as it is an invalid keyword argument for open()
with open("C:/dataanalyticsireland/Python Code/youtube/codeused/files/importandcleandatacsv.csv") as f:
emptylist=[]
for line in f:
emptylist.append(line)
df1 = pd.DataFrame(emptylist)
df1.columns = ['Temp']
df1[['Number','Error']] = df1['Temp'].str.split(',',expand=True)
#Drop the column Temp ,0 for rows and 1 for columns.
df1 = df1.drop('Temp', 1)
#Dropping the first row as it has the headers in it.
df1 = df1.drop(0)
#Resetting the index to start at value 0
df1 = df1.reset_index(drop=True)
#Replacing the Error field value that was created above witha value of 0.
df1['Error'] = df1['Error'].str.replace('0\n','0')
print("Scenario 2 printed as a dataframe")
display(df1)
#Function to loop through the dataset and see if any of the list values below are found and replace them
def run_scenario2(*l):
#This line extracts all the special characters into a new column
#Using regular expressions it finds values that should not appear
df1['Error'] = df1['Number'].str.extract('(\D+|^(0)$)')
#This line removes anything with a null value
df1.dropna(subset=['Error'], inplace=True)
#This line reads in the list and assigns it a value i, to each element of the list.
#Each i value assigned also assigns an index number to the list value.
#The index value is then used to check whether the value associated with it is in the df['Number'] column
#and then replaces if found
for i in l:
df1['Fix']= df1['Number'].str.replace(i[0],"").str.replace(i[1],"").str.replace(i[2],"").str.replace(i[3],"") \
.str.replace(i[4],"").str.replace(i[5],"").str.replace(i[6],"")
#print("Error list to check against")
#print(i[0])
#print(i[1])
#print(i[2])
#print(i[3])
#print(i[4])
#print(i[5])
#print(i[6])
print("Scenario 2 output with fixes")
display(df1)
run_scenario2(l)
############ Scenario 3 - Using read CSV #####################
#putting in engine ='python' removes any need to do additional code to display in a dataframe correctly unlike above
df2 = pd.read_csv("C:/dataanalyticsireland/Python Code/youtube/codeused/files/importandcleandatacsv.csv",engine ='python')
print("Senario 3 printed as a dataframe")
display(df2)
def run_scenario2(*l):
#This line extracts all the special characters into a new column
#Using regular expressions it finds values that should not appear
df2['Error'] = df2['Number'].str.extract('(\D+|^(0)$)')
#This line removes anything with a null value
df1.dropna(subset=['Error'], inplace=True)
#This line reads in the list and assigns it a value i, to each element of the list.
#Each i value assigned also assigns an index number to the list value.
#The index value is then used to check whether the value associated with it is in the df['Number'] column
#and then replaces if found
for i in l:
df2['Fix']= df1['Number'].str.replace(i[0],"").str.replace(i[1],"").str.replace(i[2],"").str.replace(i[3],"") \
.str.replace(i[4],"").str.replace(i[5],"").str.replace(i[6],"")
print("Scenario 3 output with fixes")
display(df2)
run_scenario2(l)