load_csv

pyhelpers.store.load_csv(path_to_csv, delimiter=',', header=0, index=None, verbose=False, **kwargs)

Load data from a CSV file.

Parameters
  • path_to_csv (str or os.PathLike[str]) – path where a CSV file is saved

  • delimiter (str) – delimiter used between values in the data file, defaults to ','

  • header (int or List[int] or None) – index number of the rows used as column names, defaults to 0

  • index (str or int or list or None) – index number of the column(s) to use as the row labels of the dataframe, defaults to None

  • verbose (bool or int) – whether to print relevant information in console, defaults to False

  • kwargs – [optional] parameters of csv.reader() or pandas.read_csv()

Returns

data retrieved from the specified path path_to_csv

Return type

pandas.DataFrame or None

Note

Examples:

>>> from pyhelpers.store import load_csv
>>> from pyhelpers.dirs import cd

>>> csv_pathname = cd("tests\data", "dat.csv")
>>> csv_dat = load_csv(csv_pathname, index=0, verbose=True)
Loading "tests\data\dat.csv" ... Done.
>>> csv_dat
             Longitude    Latitude
City
London      -0.1276474  51.5073219
Birmingham  -1.9026911  52.4796992
Manchester  -2.2451148  53.4794892
Leeds       -1.5437941  53.7974185

>>> csv_pathname = cd("tests\data", "dat.txt")
>>> csv_dat = load_csv(csv_pathname, index=0, verbose=True)
Loading "tests\data\dat.txt" ... Done.
>>> csv_dat
             Longitude    Latitude
City
London      -0.1276474  51.5073219
Birmingham  -1.9026911  52.4796992
Manchester  -2.2451148  53.4794892
Leeds       -1.5437941  53.7974185

>>> csv_dat = load_csv(csv_pathname, header=[0, 1], verbose=True)
Loading "tests\data\dat.txt" ... Done.
>>> csv_dat
         City Easting Northing
       London  530034   180381
0  Birmingham  406689   286822
1  Manchester  383819   398052
2       Leeds  582044   152953