Skip to content
Advertisement

How to merge multiple columns of a dataframe using regex?

I have a df which as following

JavaScript

Here what I want to do is combine those columns and we have two rules:

  1. If a column removes _C{0~9} or _C{0~9}{0~9} or _C{0~9}{0~9}{0~9} is equal to another column, these two columns can be combined.

Let’s take number_C1_E1 number_C2_E2 number_C3_E1 as an example, here number_C1_E1 and number_C3_E1 can be combined because they are both number_E1 after removing _C{0~9}.

  1. The two combined columns should get rid of the None values.

The desired result is

JavaScript

Anyone has a good solution?

Advertisement

Answer

Use the same as your previous question, but also compute a renamer for your columns:

JavaScript

Alternative:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement