简体   繁体   中英

Counting Cumulative Occurrences and Plotting Over Time in Pandas

Given a sample dataframe that looks like:

Time              Type
2019-12-09 04:50  Exists
2019-12-08 01:20  Does Not Exist
2019-12-08 03:32  Exists
2019-12-07 01:15  APPLES
2019-12-05 04:13  Does Not Exist

I want to cumulatively count the number of occurrences of "Exists" and "Does Not Exist", not the Occurrence of "APPLES", and plot those two values versus time. I have created the Occurrences, as shown below, but the time is not in ascending order.

  1. How do I change the time to ascending order, and then plot only "Exists" and "Does Not Exist", just in a scatter-line plot?

Thank you.

import pandas as pd

my_cols = ["Time","Type"]
df = pd.read_csv('occurrences.txt',names = my_cols,sep=';')
df['Time'] = pd.to_datetime(df['Time'])
df.set_index('Time',inplace=True)
df['Occurrence'] = df.groupby("Type").cumcount()

Filter your df first and sort_values :

new = df.loc[df['Type'].ne("APPLES")].sort_values(["Type","Time"])

new["occurance"] = new.groupby("Type").cumcount()
new.set_index("Time").groupby('Type')['occurance'].plot(legend=True)
plt.show()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM