資料範例:
期望輸出:
解說:
以下是資料程式碼,想請問怎麼寫出用if 判斷當日是否與前日同Site,再做Total_Count的累計,謝謝!
import pandas as pd
df1 = pd.DataFrame(columns=['site', 'card', 'date', 'count_record'],
data=[['A', 'C1', '12-Oct', 5],
['A', 'C1', '13-Oct', 10],
['A', 'C1', '14-Oct', 18],
['A', 'C1', '15-Oct', 21],
['A', 'C1', '16-Oct', 29],
['B', 'C2', '12-Oct', 11],
['A', 'C2', '13-Oct', 2],
['A', 'C2', '14-Oct', 7],
['A', 'C2', '15-Oct', 13],
['B', 'C2', '16-Oct', 4]])
可以把舊值存起來進行比較。
PS: 沒安裝環境就沒測試,有誤再請修正。
total = 0;
preCard = '';
preSite = '';
preCount = 0;
for row in df1:
if(card != preCard){
total = 0;
preCount = 0;
preSite = '';
}
if(site == preSite) {
total += (count - preCount);
} else {
total += count;
}
df1[Total_Count] = total;
preCount = count;
preSite = site;
preCard = card;
謝謝提供想法,以下是我的code解決了這個問題!
import pandas as pd
df1 = pd.DataFrame(columns=['site', 'card', 'date', 'count_record'],
data=[['A', 'C1', '12-Oct', 5],
['A', 'C1', '13-Oct', 10],
['A', 'C1', '14-Oct', 18],
['A', 'C1', '15-Oct', 21],
['A', 'C1', '16-Oct', 29],
['B', 'C2', '12-Oct', 11],
['A', 'C2', '13-Oct', 2],
['A', 'C2', '14-Oct', 7],
['A', 'C2', '15-Oct', 13],
['B', 'C2', '16-Oct', 4]])
def calc_total_count(df2: pd.DataFrame) -> pd.Series:
total = 0
pre_count = 0
pre_site = ''
lst = []
for c, s in zip(df2['count_record'], df2['site']):
if s == pre_site:
total += (c - pre_count)
else:
total += c
pre_count = c
pre_site = s
lst.append(total)
return pd.Series(lst, index=df2.index, name='Total_Count')
df3 = pd.concat([
df1, df1.sort_values('date').groupby('card').apply(calc_total_count).droplevel(0)
], axis=1)