Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
711 views
in Technique[技术] by (71.8m points)

python - Pandas DataFrame grouping by Timestamp

I have a use case where:

Data is of the form: Col1, Col2, Col3 and Timestamp.

Now, I just want to get the counts of the rows vs Timestamp Bins.

i.e. for every half hour bucket (even the ones which have no correponding rows), I need the counts of how many rows are there.

Timestamps are spread over a one year period, so I can't divide it into 24 buckets.

I have to bin them at 30 minutes interval.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

groupby via pd.Grouper

# optionally, if needed
# df['Timestamp'] = pd.to_datetime(df['Timestamp'], errors='coerce')  
df.groupby(pd.Grouper(key='Timestamp', freq='30min')).count()

resample

df.set_index('Timestamp').resample('30min').count()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...