Search code examples
pythonpandasfield-nameslong-format-datawide-format-data

What is the stubname for fields with multiple separators?


I'm trying to use pd.wide_to_long and I'm a little bit stumped on using the stubnames parameter:

https://pandas.pydata.org/docs/reference/api/pandas.wide_to_long.html

Here are the field names: enter image description here

Expected behavior: the transformation of a wide format df to a long format.

Current behavior: returns an empty row df with no other changes. I believe it's because of an improper use of stubnames or the sep parameter.

Attempts:

  • stubnames = "0.outbreakMap."

  • stubnames = "0.outbreakMap"

  • stubnames = "outbreakMap"

  • stubnames = "outbreakMap."

Thank you.


Solution

  • Try to swap i and j then specify the suffix (wildcard):

    >>> pd.wide_to_long(df, stubnames='0.outbreakMap', i='reportInfoId', j='id', 
                        sep='.', suffix='.*').reset_index()
    
       reportInfoId                            id 0.outbreakMap
    0          2428       1000011041.oieReference    1000011041
    1          2428  1000011041.outbreakStartDate    2010-02-03
    
    
    >>> df
       reportInfoId  0.outbreakMap.1000011041.oieReference 0.outbreakMap.1000011041.outbreakStartDate
    0          2428                             1000011041                                 2010-02-03