load_csv
- pyhelpers.store.load_csv(path_to_file, delimiter=',', header=0, index=None, verbose=False, **kwargs)[source]
Load data from a CSV file.
- Parameters:
path_to_file (str | os.PathLike) – path where a CSV file is saved
delimiter (str) – delimiter used between values in the data file, defaults to
','
header (int | List[int] | None) – index number of the rows 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 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 | None
Note
Example data can be referred to the function
pyhelpers.store.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