Use cases of the read_csv method
The
read_csv
method can be put to a variety of uses. Let us look at some such use cases.
Passing the directory address and filename as variables
Sometimes it is easier and viable to pass the directory address and filename as variables to avoid hard-coding. More importantly so, when one doesn't want to hardcode the full address of the file and intend to use this full address many times. Let us see how we can do so while importing a dataset.
import pandas as pd path = 'E:/Personal/Learning/Datasets/Book' filename = 'titanic3.csv' fullpath = path+'/'+filename data = pd.read_csv(fullpath)
For such cases, alternatively, one can use the following snippet that uses the path.join
method in an os
package:
import pandas as pd import os path = 'E:/Personal/Learning/Datasets/Book' filename = 'titanic3.csv' fullpath = os.path.join(path,filename) data = pd.read_csv(fullpath)
One advantage of using the latter method is...