long_to_wide_infill#

pandas_utils.df_handling.long_to_wide_infill(df, index_col, columns_col, values_col, index_vals=None, column_vals=None, infill=0, check_totals=False)#

Convert a DataFrame from long to wide format, infilling missing values.

Parameters:
  • df (DataFrame) – The dataframe, in long format, to convert to wide.

  • index_col (str) – The column of df to use as the index of the wide return DataFrame

  • columns_col (str) – The column of df to use as the columns of the wide return DataFrame

  • values_col (str) – The column of df to use as the values of the wide return DataFrame

  • index_vals (list[Any] | None) – The unique values to use as the index of the wide return DataFrame. If left as None, df[index_col].unique() will be used.

  • column_vals (list[Any] | None) – The unique values to use as the columns of the wide return DataFrame. If left as None, df[columns_col].unique() will be used.

  • infill (Any) – The value to use to infill any missing cells in the wide DataFrame.

  • check_totals (bool) – Whether to check if the totals are almost equal before and after the conversion.

Returns:

A copy of df, in wide format, with index_col as the index, columns_col as the column names, and values_col as the values.

Return type:

wide_df

Raises:

TypeError: – If none of the values_col is not numeric and check_totals is True