我有以下代碼從 Github 獲取資料
df = pd.read_json('https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati- json/dpc-covid19-ita-regioni.json',convert_dates =['data'])
df.index = df['data']
df.index = df.index.normalize()
df = df[df["denominazione_regione"] == 'Veneto']
在那之后(和其他下降)df 看起來像這樣:(“資料”表示“日期”,“totale_positivi”表示“完全陽性”)
data totale_positivi
2021-09-18 2
2021-09-19 5
2021-09-20 10
2021-09-21 20
2021-09-22 30
2021-09-23 40
2021-09-24 50
2021-09-25 60
2021-09-27 80
2021-09-28 100
現在我需要將此資料幀轉換為另一個資料幀,對于每個日期,該資料幀具有日期值與 7 天前的值之間的比率,如圖所示從 LATEST 值開始此操作(如果某個值不能執行該比率,簡單地將該值等于 1):
data totale_positivi
2021-09-18 1
2021-09-19 1 <--- this has no value to do the ratio, so =1 by default
2021-09-20 1 <--- this has no value to do the ratio, so =1 by default
2021-09-21 1 <--- this has no value to do the ratio, so =1 by default
2021-09-22 1 <--- this has no value to do the ratio, so =1 by default
2021-09-23 1 <--- this has no value to do the ratio, so =1 by default
2021-09-24 1 <--- this has no value to do the ratio, so =1 by default
2021-09-25 30 <--- this is 60/2 (2 is 7 days before 60)
2021-09-27 16 <--- this is 80/5 (5 is 7 days before 80)
2021-09-28 10 <--- this is 100/10 (10 is 7 days before 100)
我試過這個:
cera=len(list(df['totale_positivi']))
for i in range (0,cera):
while(i>7):
df.loc['totale_positivi'] = df.loc[cera-i] / df.loc[cera-i-7]
但它不起作用。我也試過這個:
df['totale_positivi']=df['totale_positivi'].div(periods=7)
但不起作用。
怎么解決?謝謝
uj5u.com熱心網友回復:
做:
df['totale_positivi'] = (df['totale_positivi'] / df['totale_positivi'].shift(7)).fillna(1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/343404.html
上一篇:如何從資料幀計算事件的相對頻率?
下一篇:熊貓加權統計
