Home > Mobile >  List in dataframe is different to the order it appears in the original list?
List in dataframe is different to the order it appears in the original list?

Time:04-18

So lets say I have a list of lists:

data = [[1, 2, 3, 4], [0, 2, 3, 4], [0, 0 , 3, 4], [0, 0, 0, 4]]

When I am trying to output this into a dataframe, the dataframe is appearing as follows:

df = pd.DataFrame(data)

list 1 list 2 list 3 list 4
1 2 3 4
2 3 4 0
3 4 0 0
4 0 0 0

Instead of what should appear in terms of how the 0 values are ordered in the actual list :

list 1 list 2 list 3 list 4
1 0 0 0
2 2 0 0
3 3 3 0
4 4 4 4

Does anyone have any suggestion on how to fix this?

CodePudding user response:

Each sublist in data is supposed to become a row in df, not a column, so you have to transpose data and then turn it into a df, like this:

df = pd.DataFrame(np.array(data).T)

Alternatively, if data contains strings, you could do:

df = pd.DataFrame(list(map(list, zip(*data))))

which transposes the list of lists and then turns into a df.

CodePudding user response:

IIUC, you want to "push" the 0s to the end.

You can use sorted with a custom key.

The exact output you expect is yet ambiguous as the data is symmetric, but you can transpose if needed:

data = [[1, 2, 3, 4], [0, 2, 3, 4], [0, 0 , 3, 4], [0, 0, 0, 4]]

df = (pd.DataFrame([sorted(l, key=lambda x: x==0) for l in data])
        .T # remove the ".T" of you don't want to transpose
     )

NB. If you want to push the 0s to the end and sort the other values, use lambda x: (x==0, x) as key

Output:

   0  1  2  3
0  1  2  3  4
1  2  3  4  0
2  3  4  0  0
3  4  0  0  0
  • Related