ssapy_toolkit.IO.csv_utils
Functions
|
Appends multiple CSV files into a single CSV file. |
|
Appends multiple CSV files directly to a single output file on disk. |
|
Appends data from a dictionary to a CSV file. |
|
Checks if a number exists in a specific column of a CSV file. |
|
Converts a list or dictionary into a Pandas DataFrame. |
|
Read a CSV file with options. |
|
Get the header of a CSV file. |
|
Save a Pandas DataFrame to a CSV file. |
|
Appends a single row of data to a CSV file with a specified delimiter. |
|
Saves a header row to a CSV file with a specified delimiter. |
|
Save a Pandas DataFrame to a CSV file, appending the DataFrame to the file if it exists. |
- ssapy_toolkit.IO.csv_utils.append_csv(file_names: List[str], save_path: str = 'combined_data.csv', sep: str = ',', dtypes: Dict[str, str | dtype] | None = None, progress: callable | None = None) None[source]
Appends multiple CSV files into a single CSV file.
- Args:
file_names (List[str]): A list of CSV file names. save_path (str): The path to the output CSV file. If not specified, the output will be saved to the current working directory. sep (Optional[str]): The delimiter used in the CSV files. If None, delimiter will be guessed. dtypes (Optional[Dict[str, Union[str, np.dtype]]]): A dictionary specifying data types for columns. progress (Optional[callable]): A function that can be used to track progress (e.g., printing memory usage).
- Returns:
None
Author: Travis Yeager (yeager7@llnl.gov)
- ssapy_toolkit.IO.csv_utils.append_csv_on_disk(csv_files: List[str], output_file: str) None[source]
Appends multiple CSV files directly to a single output file on disk.
- Args:
csv_files (List[str]): A list of CSV files to append. output_file (str): The path to the output CSV file.
- Returns:
None
Author: Travis Yeager (yeager7@llnl.gov)
- ssapy_toolkit.IO.csv_utils.append_dict_to_csv(file_name: str, data_dict: Dict[str, List[str | float | int]], delimiter: str = ',') None[source]
Appends data from a dictionary to a CSV file.
- Args:
file_name (str): The path to the CSV file. data_dict (Dict[str, List[Union[str, float, int]]]): A dictionary where keys are column names and values are lists of column data. delimiter (str): The delimiter used in the CSV file.
- Returns:
None
Author: Travis Yeager (yeager7@llnl.gov)
- ssapy_toolkit.IO.csv_utils.exists_in_csv(csv_file: str, column: str, number: int | float | str, sep: str = ',') bool[source]
Checks if a number exists in a specific column of a CSV file.
- Args:
csv_file (str): The path to the CSV file. column (str): The column name to search. number (Union[int, float, str]): The value to search for. sep (str, optional): The delimiter used in the CSV file. Default is comma (‘,’).
- Returns:
bool: True if the number exists in the column, False otherwise.
Author: Travis Yeager (yeager7@llnl.gov)
- ssapy_toolkit.IO.csv_utils.makedf(df: DataFrame | List | Dict) DataFrame[source]
Converts a list or dictionary into a Pandas DataFrame.
- Args:
df (Union[pd.DataFrame, List, Dict]): A DataFrame, list, or dictionary.
- Returns:
pd.DataFrame: A DataFrame created from the input.
Author: Travis Yeager (yeager7@llnl.gov)
- ssapy_toolkit.IO.csv_utils.read_csv(file_name: str, sep: str | None = None, dtypes: Dict[str, str | dtype] | None = None, col: bool | List[str] | None = False, to_np: bool = False, drop_nan: bool = False, skiprows: List[int] = []) DataFrame | ndarray[source]
Read a CSV file with options.
- Args:
file_name (str): The path to the CSV file. sep (Optional[str]): The delimiter used in the CSV file. dtypes (Optional[Dict[str, Union[str, np.dtype]]]): Dictionary specifying data types for columns. col (Union[bool, List[str], None]): Specify columns to read. to_np (bool): Convert the loaded data to a NumPy array. drop_nan (bool): Drop rows with missing values. skiprows (List[int]): Rows to skip while reading the CSV file.
- Returns:
Union[pd.DataFrame, np.ndarray]: A DataFrame or NumPy array with the loaded data.
Author: Travis Yeager (yeager7@llnl.gov)
- ssapy_toolkit.IO.csv_utils.read_csv_header(file_name: str, sep: str | None = None) List[str][source]
Get the header of a CSV file.
- Args:
file_name (str): The filename of the CSV file. sep (Optional[str]): The delimiter used in the CSV file.
- Returns:
List[str]: A list of the header fields.
Author: Travis Yeager (yeager7@llnl.gov)
- ssapy_toolkit.IO.csv_utils.save_csv(file_name: str, df: DataFrame, sep: str = ',', dtypes: Dict[str, str | dtype] | None = None) None[source]
Save a Pandas DataFrame to a CSV file.
- Args:
file_name (str): The path to the CSV file. df (pd.DataFrame): The Pandas DataFrame to save. sep (str): The delimiter used in the CSV file. dtypes (Optional[Dict[str, Union[str, np.dtype]]]): A dictionary specifying data types for columns.
- Returns:
None
Author: Travis Yeager (yeager7@llnl.gov)
- ssapy_toolkit.IO.csv_utils.save_csv_array_to_line(filename: str, array: List[str | float | int], delimiter: str = ',') None[source]
Appends a single row of data to a CSV file with a specified delimiter.
- Args:
filename (str): The name of the file to which the row will be appended. array (List[Union[str, float, int]]): A list of values representing a single row of data to be appended to the CSV file. delimiter (str, optional): The delimiter to use between columns in the CSV file. Default is comma (‘,’).
- Returns:
None
Author: Travis Yeager (yeager7@llnl.gov)
- ssapy_toolkit.IO.csv_utils.save_csv_header(filename: str, header: List[str], delimiter: str = ',') None[source]
Saves a header row to a CSV file with a specified delimiter.
- Args:
filename (str): The name of the file where the header will be saved. header (List[str]): A list of strings representing the column names. delimiter (str, optional): The delimiter to use between columns in the CSV file. Default is comma (‘,’).
- Returns:
None
Author: Travis Yeager (yeager7@llnl.gov)
- ssapy_toolkit.IO.csv_utils.save_csv_line(file_name: str, df: DataFrame, sep: str = ',', dtypes: Dict[str, str | dtype] | None = None) None[source]
Save a Pandas DataFrame to a CSV file, appending the DataFrame to the file if it exists.
- Args:
file_name (str): The path to the CSV file. df (pd.DataFrame): The Pandas DataFrame to save. sep (str): The delimiter used in the CSV file. dtypes (Optional[Dict[str, Union[str, np.dtype]]]): A dictionary specifying data types for columns.
- Returns:
None
Author: Travis Yeager (yeager7@llnl.gov)