Pandas groupby hour

The pandas library in Python provides a groupby() function which allows us to group data by a specific column or feature. To group data by hour, we can follow these steps:

  1. Import the pandas library:
  2. <script type="text/python">
    import pandas as pd
    </script>
    
  3. Create a DataFrame:
  4. <script type="text/python">
    # Creating a DataFrame
    data = {'timestamp': ['2021-01-01 12:15:00', '2021-01-01 12:30:00', '2021-01-01 13:00:00', '2021-01-01 13:30:00', '2021-01-02 10:00:00'],
            'value': [10, 5, 8, 12, 7]}
    df = pd.DataFrame(data)
    df['timestamp'] = pd.to_datetime(df['timestamp'])
    df
    </script>
    
  5. Group data by hour:
  6. <script type="text/python">
    # Grouping by hour
    df['hour'] = df['timestamp'].dt.hour
    grouped_df = df.groupby('hour').sum()
    grouped_df
    </script>
    

The resulting DataFrame, grouped_df, will show the sum of the values for each hour:

<script type="text/python">
# Output
grouped_df
</script>

Output:

   value
hour       
10       7
12      15
13      20

Leave a comment