Quantcast
Channel: Active questions tagged row - Stack Overflow
Viewing all articles
Browse latest Browse all 447

Pandas dataframe get first row of each group

$
0
0

I have a pandas DataFrame like following:

df = pd.DataFrame({'id' : [1,1,1,2,2,3,3,3,3,4,4,5,6,6,6,7,7],'value'  : ["first","second","second","first","second","first","third","fourth","fifth","second","fifth","first","first","second","third","fourth","fifth"]})

I want to group this by ["id","value"] and get the first row of each group:

        id   value0        1   first1        1  second2        1  second3        2   first4        2  second5        3   first6        3   third7        3  fourth8        3   fifth9        4  second10       4   fifth11       5   first12       6   first13       6  second14       6   third15       7  fourth16       7   fifth

Expected outcome:

id   value 1   first 2   first 3   first 4  second 5  first 6  first 7  fourth

I tried following, which only gives the first row of the DataFrame.

In [25]: for index, row in df.iterrows():   ....:     df2 = pd.DataFrame(df.groupby(['id','value']).reset_index().ix[0])

Viewing all articles
Browse latest Browse all 447

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>