How to set a list as the index of an existing pandas dataframe?
Question:
I have an existing pandas dataframe, which looks something like:
Gr R B Gb
0 1.537037 1.733333 1.400000 1.537037
1 1.657952 1.706522 1.650407 1.713024
2 1.670982 1.690476 1.626263 1.661202
3 1.689229 1.683761 1.607143 1.676923
...
I have a separate list, where the length of the list is equal to the number of rows in the above dataframe. Something like:
separate_list = ['a','b','c',....]
How can I make the separate list to be the index of the above dataframe, replacing the default index (0,1,2,…)? All I can find are ways to set an existing column to the index.
Thank you.
Answers:
Simply making your list a column of the DataFrame (df['new_index'] = separate_list
) and then setting to be the index should work.
A pandas df has a set_index
method that allows you to make an existing column or a separate series the new df index.
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.set_index.html
Dataframe.set_index() will help you to do the same.
For your example, it will be,
DF = pd.DataFrame(#Your Dataframe)
DF.set_index(keys=separate_list)
Let DF
be your pandas.DataFrame
and index_list
be a flat list with same size as DF rows, something like index_list = [0,1,2,3,4,5,6,...]
:
DF.set_index(keys = [index_list])
will do it. And note that index_list must be enclosed between brackets in this case.
Alternatively, if index_list = [[0,1,2,3,4,5,6,...]]
you should use:
DF.set_index(keys = index_list)
and now, there is no need to enclose it, that’s the way it goes with Shah’s example above.
I have an existing pandas dataframe, which looks something like:
Gr R B Gb
0 1.537037 1.733333 1.400000 1.537037
1 1.657952 1.706522 1.650407 1.713024
2 1.670982 1.690476 1.626263 1.661202
3 1.689229 1.683761 1.607143 1.676923
...
I have a separate list, where the length of the list is equal to the number of rows in the above dataframe. Something like:
separate_list = ['a','b','c',....]
How can I make the separate list to be the index of the above dataframe, replacing the default index (0,1,2,…)? All I can find are ways to set an existing column to the index.
Thank you.
Simply making your list a column of the DataFrame (df['new_index'] = separate_list
) and then setting to be the index should work.
A pandas df has a set_index
method that allows you to make an existing column or a separate series the new df index.
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.set_index.html
Dataframe.set_index() will help you to do the same.
For your example, it will be,
DF = pd.DataFrame(#Your Dataframe)
DF.set_index(keys=separate_list)
Let DF
be your pandas.DataFrame
and index_list
be a flat list with same size as DF rows, something like index_list = [0,1,2,3,4,5,6,...]
:
DF.set_index(keys = [index_list])
will do it. And note that index_list must be enclosed between brackets in this case.
Alternatively, if index_list = [[0,1,2,3,4,5,6,...]]
you should use:
DF.set_index(keys = index_list)
and now, there is no need to enclose it, that’s the way it goes with Shah’s example above.