3

I have tried to choose a column to be an index of a data frame. The examples I've seen so far suggests to use the method set_index(), but it doesn't work in my case. I use python 3.7.0

import pandas as pd

df = pd.DataFrame({'Fruit' : ['Apples','Oranges'],
                   'Amount': [ 1, 17 ]})

df.set_index('Fruit')


print(df1)

The output that I get is

       Fruit  Amount
0   'Apples'       1
1  'Oranges'      17

The output I want would be something like

              Amount
     Fruit  
    'Apples'       1
   'Oranges'      17
3
  • 2
    df = df.set_index('Fruit') Commented Mar 20, 2019 at 14:23
  • 3
    or df.set_index('Fruit', inplace=True) Commented Mar 20, 2019 at 14:25
  • {inplace=True} makes the difference! Thank you! Commented Mar 20, 2019 at 14:31

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.