code: def pd_to_tabular_cpd(cpd: pd.DataFrame) -> TabularCPD:
      """
      Converts a dataframe to a pgmpy TabularCPD
  
      Args:
          cpd: Pandas dataframe containing conditional probability distribution (CPD)
  
      Returns:
          Corresponding tabular CPD
      """
      parents = cpd.columns.names
  
      if (parents is None) or all(el is None for el in parents):
          parents = None
          parents_cardinalities = None
          state_names = {}
      else:
          parents_cardinalities = [
              len(level) for level in cpd.columns.levels
          ]
          state_names = {
              name: list(levels)
              for name, levels in zip(
                  cpd.columns.names, cpd.columns.levels
              )
          }
  
      node_cardinality = cpd.shape[0]
      node_name = cpd.index.name
      state_names[node_name] = list(cpd.index)
  
      return TabularCPD(
          node_name,
          node_cardinality,
          cpd.values,
          evidence=parents,
          evidence_card=parents_cardinalities,
          state_names=state_names,
      )
  
imports: from typing import List
  import pandas
  from pgmpy.factors import factor_product
  from pgmpy.factors.discrete import TabularCPD
label_desc: Use image node_id 1 for calling a global function with example usage: pd_to_tabular_cpd(cpd) and returns: TabularCPD