How do I get the first timestamp (index) of a group when applying groupby to a python pandas dataframe?

user1412286

My data in principle looks like this:

                            one  two
timestamp                           
2013-12-06 00:00:01.200000    1    1
2013-12-06 00:00:02.200000    1    2
2013-12-06 00:00:03.200000    2    1
2013-12-06 00:00:04.200000    3    5
2013-12-06 00:00:05.200000    1    2

I would like to group it over column 'one' and take the first timestamp of each group. Applying this to column 'two' works just fine but it does not work for the timestamp.

df_2 = df['two'].groupby(df['one']).first()

gives:

one
1      1
2      1
3      5

but it tells me there is no attribute 'first' when I apply the same thing to the index.

df_3 = df.index.groupby(df['one']).first()

Does anyone know how this can be done?

unutbu

You could use groupby/apply:

>>> grouped = df.groupby('one')
>>> grouped.apply(lambda x: x.index[0])
one
1     2013-12-06 00:00:01.200000
2     2013-12-06 00:00:03.200000
3     2013-12-06 00:00:04.200000
dtype: datetime64[ns]

By the way,

df_2 = df['two'].groupby(df['one']).first()

can also be expressed as

>>> grouped['two'].first()
one
1      1
2      1
3      5
Name: two, dtype: int64

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I get the index of each item in a groupby object in Pandas?

From Dev

Python Pandas, setting groupby() group labels as index in a new dataframe

From Dev

Pandas : Python how to do index Groupby with replicates

From Dev

How do I turn pandas DataFrame groupby results into a DataFrame?

From Dev

pandas: how do I select first row in each GROUP BY group?

From Dev

How can I slice a dataframe by timestamp, when timestamp isn't classified as index?

From Java

How to group dataframe rows into list in pandas groupby

From Java

Pandas dataframe get first row of each group

From Dev

Python Pandas: Groupby date, and accessing each group by timestamp

From Dev

Python Pandas: Groupby date, and accessing each group by timestamp

From Dev

How do I group lat-lon pairings in a Pandas DataFrame?

From Dev

Pandas dataframe index causing problems when indexing subset of dataframe. How do I remove the indexes, or prevent the error from occurring?

From Dev

how do i get last result in group by MySQL query (not the first)

From Dev

How do I get the first group of alpha characters from string?

From Dev

How do I refer to the index of my Pandas dataframe?

From Dev

How do i change a single index value in pandas dataframe?

From Dev

How to get last group in Pandas' groupBy?

From Dev

Pandas: how to get a particular group after groupby?

From Dev

How do i get the value of first index in a returned List of array

From Dev

Python Pandas: How do I return members of groupby

From Dev

Python Pandas: How do I return members of groupby

From Dev

How to Update Value in First N Rows by Group in a Multi-Index Pandas Dataframe?

From Dev

pandas dataframe group by uneven timestamp

From Dev

Pandas DataFrame groupby two columns and get first and last

From Dev

Pandas DataFrame groupby two columns and get first and last

From Dev

Applying iterative function to every group in pandas DataFrame

From Dev

Applying iterative function to every group in pandas DataFrame

From Dev

How do I get all the rows before a specific index in Pandas?

From Dev

How can I concatenate date from another column when I use groupby and aggregation in a pandas dataframe

Related Related

  1. 1

    How do I get the index of each item in a groupby object in Pandas?

  2. 2

    Python Pandas, setting groupby() group labels as index in a new dataframe

  3. 3

    Pandas : Python how to do index Groupby with replicates

  4. 4

    How do I turn pandas DataFrame groupby results into a DataFrame?

  5. 5

    pandas: how do I select first row in each GROUP BY group?

  6. 6

    How can I slice a dataframe by timestamp, when timestamp isn't classified as index?

  7. 7

    How to group dataframe rows into list in pandas groupby

  8. 8

    Pandas dataframe get first row of each group

  9. 9

    Python Pandas: Groupby date, and accessing each group by timestamp

  10. 10

    Python Pandas: Groupby date, and accessing each group by timestamp

  11. 11

    How do I group lat-lon pairings in a Pandas DataFrame?

  12. 12

    Pandas dataframe index causing problems when indexing subset of dataframe. How do I remove the indexes, or prevent the error from occurring?

  13. 13

    how do i get last result in group by MySQL query (not the first)

  14. 14

    How do I get the first group of alpha characters from string?

  15. 15

    How do I refer to the index of my Pandas dataframe?

  16. 16

    How do i change a single index value in pandas dataframe?

  17. 17

    How to get last group in Pandas' groupBy?

  18. 18

    Pandas: how to get a particular group after groupby?

  19. 19

    How do i get the value of first index in a returned List of array

  20. 20

    Python Pandas: How do I return members of groupby

  21. 21

    Python Pandas: How do I return members of groupby

  22. 22

    How to Update Value in First N Rows by Group in a Multi-Index Pandas Dataframe?

  23. 23

    pandas dataframe group by uneven timestamp

  24. 24

    Pandas DataFrame groupby two columns and get first and last

  25. 25

    Pandas DataFrame groupby two columns and get first and last

  26. 26

    Applying iterative function to every group in pandas DataFrame

  27. 27

    Applying iterative function to every group in pandas DataFrame

  28. 28

    How do I get all the rows before a specific index in Pandas?

  29. 29

    How can I concatenate date from another column when I use groupby and aggregation in a pandas dataframe

HotTag

Archive