Home > database >  How to order the dataframe'columns names the same way of another dataframe
How to order the dataframe'columns names the same way of another dataframe

Time:07-17

I have a dataframe like this: DF1

text    yellow   blue    red    orange
dfsfs     0       1       1       0
sdfsdf    0       0       1       1
asdasd    1       1       1       1

and Another dataframe like this: DF2

    text    blue   orange    pink   red   black  yellow
    dfsfs     0       1       1        0     0      1
    sdfsdf    0       0       1        1     0      1
    asdasd    1       1       1        1     0      0

What's the best way to order the second dataframe'columns(DF2) in the same order we have in the first dataframe (DF1) considering the names that matchs and the names that dosen't match in last positions?

The output shoud be:

text    yellow   blue    red    orange  black  pink
dfsfs     0       1       1       0      0      1
sdfsdf    0       0       1       1      0      1
asdasd    1       1       1       1      0      1

CodePudding user response:

We can use sort by key and supply index to the sorting algorithm where we want our data to be based on the second list.

def position(value):
    try:
        return X.index(value)
    except ValueError:
        return len(X)

X = df1.columns.tolist()
Y = df2.columns.tolist()
Y.sort(key=position)

The columns from second dataframe is ['yellow', 'blue', 'red', 'orange', 'pink', 'black']. Now we can rearrange our database columns using :

df2 = df2[Y]

This gives us the expected output :

text    yellow   blue    red    orange  black  pink
dfsfs     0       1       1       0      0      1
sdfsdf    0       0       1       1      0      1
asdasd    1       1       1       1      0      1

CodePudding user response:

You can do:

df2[df1.columns.append(df2.columns.drop(df1.columns))]

output:

     text  yellow  blue  red  orange  pink  black
0   dfsfs       1     0    0       1     1      0
1  sdfsdf       1     0    1       0     1      0
2  asdasd       0     1    1       1     1      0
  • Related