newbie
newbie

Reputation: 644

set_index by the index of the column PYTHON

can I use dataframe.set_index with the index of the column or it only works with the name of the column??

Example:

df4 = df.set_index(0).T instead of df4 = df.set_index('Parametres').T

thank you

Upvotes: 1

Views: 154

Answers (1)

jezrael
jezrael

Reputation: 862431

If want create new index by first column use indexing:

df = pd.DataFrame({
        'A':list('abcdef'),
         'B':[4,5,4,5,5,4],
         'C':[7,8,9,4,2,3],
})

print (df.columns[0])
A

df = df.set_index(df.columns[0])
print (df)
   B  C
A      
a  4  7
b  5  8
c  4  9
d  5  4
e  5  2
f  4  3

Upvotes: 2

Related Questions