Search code examples
pythonpandasdataframepandas-melt

Pandas: how to unpivot df correctly?


I have the following dataframe df:

  A  B  Var    Value
0 A1 B1 T1name T1
1 A2 B2 T1name T1
2 A1 B1 T2name T2
3 A2 B2 T2name T2
4 A1 B1 T1res  1
5 A2 B2 T1res  1
6 A1 B1 T2res  2
7 A2 B2 T2res  2

I now want to 'half' my dataframe because Var contains variables that should not go under the same column. My intended outcome is:

  A  B  Name   Value
0 A1 B1 T1     1
1 A2 B2 T1     1
2 A1 B1 T2     2
3 A2 B2 T2     2

What should I use to unpivot this correctly?


Solution

  • Just filter where the string contains res and assign a new column with the first two characters of the var columns

    df[df['Var'].str.contains('res')].assign(Name=df['Var'].str[:2]).drop(columns='Var')
    
        A   B Value Name
    4  A1  B1     1   T1
    5  A2  B2     1   T1
    6  A1  B1     2   T2
    7  A2  B2     2   T2
    

    Note that this creates a slice of the original DataFrame and not a copy