Search code examples
pythonpandasdataframevector

create a vector (multiple columns in one new column) pandas


Please, i have a dataframe like this:

data = {
    "a": [420, 380, 390],
    "b": [50, 40, 45],
    "c": [30, 22, 11],
    "d": [323, 2, 33]
}

df = pd.DataFrame(data)
a   b   c    d
0  420  50  30  323
1  380  40  22    2
2  390  45  11   33

and i want to get this result:

    a   d
        b   c    d
0  420  50  30  323
1  380  40  22    2
2  390  45  11   33

do you have any suggestions ?

i tried merge and concatenate but not working for me


Solution

  • Try this using pd.MultiIndex:

    df.columns = pd.MultiIndex.from_arrays([[*'addd'],[*' bcd']])
    

    Output:

         a   d         
             b   c    d
    0  420  50  30  323
    1  380  40  22    2
    2  390  45  11   33