wide_to_long_infill#

pandas_utils.df_handling.wide_to_long_infill(df, index_col_1_name, index_col_2_name, value_col_name, index_col_1_vals=None, index_col_2_vals=None, infill=0, check_totals=False)#

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

Parameters:
  • df (DataFrame) – The dataframe, in wide format, to convert to long. The index of df must be the values that are to become index_col_1_name, and the columns of df will be melted to become index_col_2_name.

  • index_col_1_name (str) – The name to give to the column that was the index of df.

  • index_col_2_name (str) – The name to give to the column that was the column names of df.

  • value_col_name (str) – The name to give to the column that was the values of df.

  • index_col_1_vals (list[Any] | None) – The unique values to use as the first index of the return dataframe. These unique values will be combined with every combination of index_col_2_vals to create the full index. If left as None, the unique values of df Index will be used.

  • index_col_2_vals (list[Any] | None) – The unique values to use as the second index of the return dataframe. These unique values will be combined with every combination of index_col_1_vals to create the full index. If left as None, the unique values of df columns will be used.

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

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

Returns:

A copy of df, in long format, with 3 columns: [index_col_1_name, index_col_2_name, value_col_name]

Return type:

long_df

Raises:

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