taskforce_ham
taskforce_ham

Reputation: 3

How can I set the index of a generated pandas Series to a column from a DataFrame?

In pandas this operation creates a Series:

q7.loc[:, list(q7)].max(axis=1) - q7.loc[:, list(q7)].min(axis=1)

I would like to be able to set the index as a list of values from a df colum. Ie

list(df['Colname'])

I've tried to create the series then update it with the series generated from the first code snippet. I've also searched the docs and don't see a method that will allow me to do this. I would prefer not to manually iterate over it.

Help is appreciated.

Upvotes: 0

Views: 208

Answers (2)

Sreeram TP
Sreeram TP

Reputation: 11907

You can simply store that series to a variable say S and set the index accordingly as shown below..!!

S = (q7.loc[:, list(q7)].max(axis=1) - q7.loc[:, list(q7)].min(axis=1))

S.index = df['Colname']

The code is provided assuming the lengths of the series and Column from the dataframe is equal. Hope this helps.!!

Upvotes: 1

Grigoriy Mikhalkin
Grigoriy Mikhalkin

Reputation: 5573

If you want to reset series s index, you can do:

s.index = new_index_list

Upvotes: 1

Related Questions