Date index grouping in pandas

I have a dataframe that looks like this:

   In [101]:

import pandas as pd
df = pd.DataFrame( {'date':['2014-06-30','2014-06-30','2014-06-29','2014-06-29','2014-06-29'], 'value':[1,2,5,5,4]})
df.set_index('date')
Out[101]:
         value
date    
2014-06-30  1
2014-06-30  2
2014-06-29  5
2014-06-29  5
2014-06-29  4

      

Here I want to group the index column based on the date value. The desired output should be like this:

df
Out[102]:
           value
date    
2014-06-30  1
            2
2014-06-29  5
            5
            4

      

So, if I use df.iloc [0], the output should be like this:

2014-06-30      1
                2

      

+3


source to share


1 answer


df.set_index('date')

must be assigned df

and you use.loc

df = pd.DataFrame( {'date':['2014-06-30','2014-06-30','2014-06-29','2014-06-29','2014-06-29'], 'value':[1,2,5,5,4]})
df = df.set_index('date')
df

            value
date
2014-06-30      1
2014-06-30      2
2014-06-29      5
2014-06-29      5
2014-06-29      4

      



Use .loc

df.loc['2014-06-30']

            value
date
2014-06-30      1
2014-06-30      2

      

0


source







All Articles