我有一個字段t1_months
設置爲計算花費發生的月數的值。我對這個價值很有信心,並且希望將其用作限制要分配的分配數量。使用條件來執行numpy與熊貓
有了這個代碼,我得到的錯誤ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
所以這使我認爲,我需要重新裝備這一行,因爲這是代碼停就行了。
if count <= counter:
我該如何去創建一個像這樣的條件,但仍然能夠使用np.where子句?
for month_num in range(1, 13):
count = 0
# counter = df['t1_months']
if count <= df['t1_months']:
if count <= counter:
df['t1_' + str(month_num) + '/1/2016'] = np.where(
(df["StartMonthYear"] <= pd.to_datetime(str(month_num) + '/1/2016'))
& (df["EndMonthYear"] >= pd.to_datetime(str(month_num) + '/1/2016')), df["t1_allotment"], '0.0')
count += 1
所以,如果我有df = pd.DataFrame({'t1_months' : [12,10,6,7]})
數據幀我怎麼能使用12作爲第一行中的檢查只填充12間分配,10第二排,第三6,7在最後?
預計產出將是這樣的:
t1_months 1/1/2016 ... 6/1/2016 7/1/2016 8/1/2016 9/1/2016 10/1/2016 11/1/2016 12/1/2016
12 500 ... 500 500 500 500 500 500 500
10 500 ... 500 500 500 500 500 500
500 500 0 0
6 500 ... 500 500 500 500 0 0 0 0 0 0
7 500 ... 500 500 500 500 500 0 0 0 0 0
'DF [「t1_months」]'是熊貓一整列因此如何應該比較,如果該列包含多個不同的值以完成? – roganjosh
你的錯誤在這裏產生:'if count <= df ['t1_months']'count是一個int,df ['t1_months]是一個序列。這些不能比較 –
@WoodyPride&roganjosh這就是我試圖與之合作。我希望數據框中每行的值作爲檢查。然後,一旦這一行已經解決,我該如何移動到下一行,下一個等...抱歉,我的無知,我與熊貓有點新。 – mnickey