Vondoe79
Vondoe79

Reputation: 325

How do I change the index of a pandas dataframe object so as not to get null values in the dataframe entries?

I do not seem to know what the issue is when I combined three dataframes into one and tried changing the index of the combined dataframe. The following is what I have done:

1) I first combined (or Concatenated) three dataframes into a 'combo' dataframe. Below is an excerpt ('TSP_JuMP_Obtained_Solu') of one of the three. The index goes from 0-9 for all the three datafames as well as the combined.

enter image description here

2) I then used the following line of code to combine them:

f_solu_tsp = pd.concat([list_TSP,list_Scenario1,list_Scenario2], axis=1, 
sort=True)

3) I subsequently used the followine line of code to change the index of the combined dataframe (df_solu_tsp):

df_solu_tsp = df_solu_tsp.reindex(proTy_uniq_list)

NB: 'proTy_uniq_list' is a list with membership as shown below:

[u'lau15', u'gr17', u'fri26', u'bays29', u'dantzig42', u'KATRINA_38', 
u'HARVEY_50', u'HARVEY_100', u'HARVEY_200', u'HARVEY_415']

Below is the result of the combined dataframe (df_solu_tsp ):

enter image description here

Thank you in advance for the help.

Upvotes: 0

Views: 21

Answers (1)

Erfan
Erfan

Reputation: 42916

Without having example DataFrame I will try to answer as good as possible:

Solution 1

As Peter Leimbigler mentioned in the comments:

df_solu_tsp = df_solu_tsp.set_index(proTy_uniq_list)

Which replaces your original index with the new index which is in this case an equal length list.

Solution 2

As mentioned in the pandas docs

df_solu_tsp.set_index([pd.Index(proTy_uniq_list), 'proTy'])

Solution 3

I see that you're creating a dataframe from three lists, so we can go a step back and create your data in one go:

f_solu_tsp  = pd.DataFrame({'TSP_JuMP_Obtained_Solu': list_TSP,
                            'Scenario1': list_Scenario1,
                            'Scenario2': list_Scenario2}, index=proTy_uniq_list)

Example solution 3

data1 = ['hi', 'goodbye']
data2 = ['hello', 'bye']

idx = ['arriving', 'leaving']

df = pd.DataFrame({'column1': data1,
                   'column2': data2}, index=idx)

print(df)
          column1 column2
arriving       hi   hello
leaving   goodbye     bye

Upvotes: 1

Related Questions