簡體   English   中英

計算 Pandas 中的累積發生次數並繪制隨時間變化的圖

[英]Counting Cumulative Occurrences and Plotting Over Time in Pandas

給定一個示例數據框,如下所示:

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

我想累計計算“存在”和“不存在”的出現次數,而不是“蘋果”的出現次數,並繪制這兩個值與時間的關系圖。 我已經創建了 Occurrences,如下所示,但時間不是按升序排列的。

  1. 如何將時間更改為升序,然后僅在散點圖中繪制“存在”和“不存在”?

謝謝你。

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()

首先過濾您的 df 和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()

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM