3

I have the following DataFrame:

df = pd.DataFrame({
'Trader': 'Carl Mark Carl Joe Joe Carl Joe Carl'.split(),
'Product': list('AAAABBAA'),
'Quantity': [5,2,5,10,1,5,2,3],
'Start' : [
    DT.datetime(2013,1,1,9,0),
    DT.datetime(2013,1,1,8,5),
    DT.datetime(2013,2,5,14,0),
    DT.datetime(2013,2,5,16,0),
    DT.datetime(2013,2,8,20,0),                                      
    DT.datetime(2013,2,8,16,50),
    DT.datetime(2013,2,8,7,0),
    DT.datetime(2013,7,4,8,0)]})

When I try to put the index via:

df = df.set_index([df.Start, df.Trader, df.Product])

It does not delete the columns which are used for the index. Even when I specify it explicitly using:

df = df.set_index([df.Start, df.Trader, df.Product], drop=True)

Is this a bug or do I have a mistake in my code?

Thanks

Andy

1 Answer 1

7

The parameter is an array of column names but not those series:

In [9]: df.set_index(['Start', 'Trader', 'Product'])
Out[9]: 
                                    Quantity
Start               Trader Product          
2013-01-01 09:00:00 Carl   A               5
2013-01-01 08:05:00 Mark   A               2
2013-02-05 14:00:00 Carl   A               5
2013-02-05 16:00:00 Joe    A              10
2013-02-08 20:00:00 Joe    B               1
2013-02-08 16:50:00 Carl   B               5
2013-02-08 07:00:00 Joe    A               2
2013-07-04 08:00:00 Carl   A               3
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the clarification. I am a Pandas beginner and it was not clear to me to see the differences between the two.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.