I have a DataFrame called mcmc_samples which contains Markov-Chain-Monte-Carlo Samples for multiple variables
| deviance | K_1[1,1] | K_1[1,2] | K_1[1,3] | K_1[2,1] | K_1[2,2] | K_1[2,3] |
|---|---|---|---|---|---|---|
| 0.2 | 0.4 | 0.6 | 0.1 | 0.3 | 0.9 | 0.8 |
| ... | ... | ... | ... | ... | ... | ... |
The columns names are composed of the level (K_1), the variable (first number in brackets before comma) and the category (second number in brackets after comma).
I try to rename the column names such that the numbers in the brackets are more meaningful. For this purpose, I want to use the following dictionaries.
dict_var = {1: "variable_1", 2: "variable_2"}
dict_categ = {1: "item_1, 2: "item_2", 3: "item_3"}
I tried to replace the strings using regular expression
mcmc_samples.columns = mcmc_samples.columns.str.replace(r"(?<=,)(.*?)(?=\])",
mcmc_samples.columns.str.extract(r"(?<=,)(.*?)(?=\])")[0].map(dict_categ), regex=True)
but this gave me the following error:
TypeError: repl must be a string or callable