Home > database >  Why are my Pandas DataFrame values out of order according to the index?
Why are my Pandas DataFrame values out of order according to the index?

Time:09-21

Problem:

Executing this code:

import pandas as pd

data = {"1","2","3","4","5"}
index = ["1_i","2_i","3_i","4_i","5_i"]

df = pd.DataFrame(data,index=index)

print(df)

Results in this output:

     0
1_i  4
2_i  3
3_i  5
4_i  1
5_i  2

Question: Why aren't the values in order according to the index that I set it to? 1 should be set to the index 1_i, 2 should be set to the index 2_i, etc.

CodePudding user response:

The problem is that you are making the dataframe from a set, which is arbitrarily ordered. Try making your dataframe from a container that maintains order, like a list or tuple.

import pandas as pd

data = ["1","2","3","4","5"]  # a list
index = ["1_i","2_i","3_i","4_i","5_i"]

df = pd.DataFrame(data, index=index)
  • Related