I have the following data frame: enter image description here enter image description here
***>> df = pd.DataFrame(
{
"Tool": [1 , 2 , 1 , 2, 2, 2, 1, 2, 1,2],
"Timestamp": ['2022-05-28 05:03:00','2022-05-29 06:07:00','2022-05-28 07:11:00','2022-05-27 11:04:00','2022-05-27 05:43:00','2022-05-25 06:07:00','2022-05-28 11:22:00','2022-05-28 02:00:00','2022-05-26 04:22:00','2022-05-28 09:55:00'],
"Sensor1": [10,11,12,13,6,7,3,11,8,9],
"Sensor2": [5,4,3,3,5,4,3,3,8,2],
"Sensor3": [1,2,3,6,1,2,3,6,4,5],
}
)
>>df.groupby(['Tool', 'Timestamp'])[['Sensor1', 'Sensor2', 'Sensor3']].mean()***
But I'd like to group by the tool first and secondly group the sensor values by the same day (sensor value is mean value). Thanks
chatGPT provided suitable solution but I don't understand where appear parameter saying to group by day:
import pandas as pd
# Your DataFrame
df = pd.DataFrame(
{
"Tool": [1 , 2 , 1 , 2, 2, 2, 1, 2, 1,2],
"TimeStamp": ['2022-05-28 05:03:00','2022-05-29 06:07:00','2022-05-28 07:11:00','2022-05-27 11:04:00',
'2022-05-27 05:43:00','2022-05-25 06:07:00','2022-05-28 11:22:00','2022-05-28 02:00:00',
'2022-05-26 04:22:00','2022-05-28 09:55:00'],
"Sensor1": [10,11,12,13,6,7,3,11,8,9],
"Sensor2": [5,4,3,3,5,4,3,3,8,2],
"Sensor3": [1,2,3,6,1,2,3,6,4,5],
}
)
# Convert TimeStamp to datetime
df['TimeStamp'] = pd.to_datetime(df['TimeStamp'])
# Extract the date part of the timestamp
df['Date'] = df['TimeStamp'].dt.date
# Group by 'Tool' and 'Date', and calculate the mean of Sensor1, Sensor2, and Sensor3
result = df.groupby(['Tool', 'Date']).agg({
'Sensor1': 'mean',
'Sensor2': 'mean',
'Sensor3': 'mean'
}).reset_index()
print(result)