我有一個相當具體的算法,我想遵循。根據行和列條件設置熊貓數據幀值
基本上我有一個數據幀如下:
month taken score
1 1 2 23
2 1 1 34
3 1 2 12
4 1 2 59
5 2 1 12
6 2 2 23
7 2 1 43
8 2 2 45
9 3 1 43
10 3 2 43
11 4 1 23
12 4 2 94
我想讓它使「分數」一欄更改爲100,其中採取== 2持續,直到月底天。所以,沒有采取== 2中所有出現有其得分爲100,如果在一個月之後的任何一天都有一個採取== 1
所以結果我想要的是:
month taken score
1 1 2 23
2 1 1 34
3 1 2 100
4 1 2 100
5 2 1 12
6 2 2 23
7 2 1 43
8 2 2 100
9 3 1 43
10 3 2 43
11 3 1 23
12 3 2 100
13 4 1 32
14 4 2 100
我寫這個代碼,我覺得應該這樣做:
#iterate through months
for month in range(12):
#iterate through scores
for score in range(len(df_report.loc[df_report['month'] == month+1])):
#starting from the bottom, of that month, if 'taken' == 2...
if df_report.loc[df_report.month==month+1, 'taken'].iloc[-score-1] == 2:
#then set the score to 100
df_report.loc[df_report.month==month+1, 'score'].iloc[-score-2] = 100
#if you run into a 'taken' == 1, move on to next month
else: break
然而,這似乎並沒有更改任何值,儘管不引發錯誤...它也沒有給我一個錯誤關於將值設置爲複製的數據幀。
任何人都可以解釋我做錯了什麼嗎?
如果我猜的話,這將是你用新值設置副本。鏈接* loc呼叫不是最好的主意。 –
我認爲你是對的,但我該如何解決這個問題?另外,如果.loc不是副本,並且.iloc不是副本,那麼爲什麼.loc的.iloc是副本?! – James