Reputation: 341
I would like to set an index based on two columns. The two columns are named 'name' and 'date'.
This is the code that I have so far:
df = pd.DataFrame(index=df[['name', 'date']]).reset_index().rename(
columns={'index': 'name'})
Could you please advise me?
Cheers, Jeroen
Upvotes: 1
Views: 998
Reputation: 20674
I believe that is what you mean.
df = df.set_index(['col1', 'col2'])
To pass more argument (e.g. inplace, drop columns etc.) check this link for more information: pandas documentation
Upvotes: 1
Reputation: 6784
Like this?
In [42]: df = pd.DataFrame({'name': ['foo', 'bar'], 'date': ['2012', '2017']})
In [43]: df
Out[43]:
date name
0 2012 foo
1 2017 bar
In [44]: df.set_index(df.apply(lambda x: tuple(x)))
Out[44]:
date name
(2012, 2017) 2012 foo
(foo, bar) 2017 bar
Upvotes: 0