load_csv

pyhelpers.store.load_csv(path_to_file, delimiter=',', header=0, index=None, verbose=False, prt_kwargs=None, **kwargs)[source]

Load data from a CSV file.

Parameters:
  • path_to_file (str | os.PathLike) – Pathname of the CSV file.

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

  • header (int | List[int] | None) – Index number of the row(s) used as column names; defaults to 0.

  • index (str | int | list | None) – Index number of the column(s) to use as the row labels of the dataframe; defaults to None.

  • verbose (bool | int) – Whether to print relevant information to the console; defaults to False.

  • prt_kwargs (dict | None) – [Optional] Additional parameters for the function pyhelpers.store.ldr._check_loading_path(); defaults to None.

  • kwargs – [Optional] Additional parameters for csv.reader() or pandas.read_csv().

Returns:

Data retrieved from the specified path path_to_file.

Return type:

pandas.DataFrame | None

Note

Example data can be referred to in the function save_spreadsheet().

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