2017-10-09 80 views
1

我有一個字段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 
+1

'DF [「t1_months」]'是熊貓一整列因此如何應該比較,如果該列包含多個不同的值以完成? – roganjosh

+1

你的錯誤在這裏產生:'if count <= df ['t1_months']'count是一個int,df ['t1_months]是一個序列。這些不能比較 –

+0

@WoodyPride&roganjosh這就是我試圖與之合作。我希望數據框中每行的值作爲檢查。然後,一旦這一行已經解決,我該如何移動到下一行,下一個等...抱歉,我的無知,我與熊貓有點新。 – mnickey

回答

1

來源DF:

In [77]: d 
Out[77]: 
    t1_months 
0   12 
1   10 
2   6 
3   7 

選項1:

In [78]: d.join(d.t1_months.apply(lambda x: pd.Series([500]*x)).fillna(0)) \ 
    ...: .rename(columns=lambda x: '{}/1/2016'.format(x+1) if isinstance(x,int) else x) 
Out[78]: 
    t1_months 1/1/2016 2/1/2016 3/1/2016 4/1/2016 5/1/2016 6/1/2016 7/1/2016 8/1/2016 9/1/2016 10/1/2016 11/1/2016 \ 
0   12  500.0  500.0  500.0  500.0  500.0  500.0  500.0  500.0  500.0  500.0  500.0 
1   10  500.0  500.0  500.0  500.0  500.0  500.0  500.0  500.0  500.0  500.0  0.0 
2   6  500.0  500.0  500.0  500.0  500.0  500.0  0.0  0.0  0.0  0.0  0.0 
3   7  500.0  500.0  500.0  500.0  500.0  500.0  500.0  0.0  0.0  0.0  0.0 

    12/1/2016 
0  500.0 
1  0.0 
2  0.0 
3  0.0 

選項2:

In [87]: d.join(pd.DataFrame([[500]*x for x in d.t1_months], 
    ...:      columns=['{}/1/2016'.format(i) for i in range(1,13)], 
    ...:      index=d.index)) 
    ...: 
Out[87]: 
    t1_months 1/1/2016 2/1/2016 3/1/2016 4/1/2016 5/1/2016 6/1/2016 7/1/2016 8/1/2016 9/1/2016 10/1/2016 11/1/2016 \ 
0   12  500  500  500  500  500  500  500.0  500.0  500.0  500.0  500.0 
1   10  500  500  500  500  500  500  500.0  500.0  500.0  500.0  NaN 
2   6  500  500  500  500  500  500  NaN  NaN  NaN  NaN  NaN 
3   7  500  500  500  500  500  500  500.0  NaN  NaN  NaN  NaN 

    12/1/2016 
0  500.0 
1  NaN 
2  NaN 
3  NaN 

UPDATE:

說,我這個DF雖然。

df = pd.DataFrame({'months': [12,10,6,7], 'allot': [200, 500, 347, 192]})

如何更換與這是在DF [「配發」]行 值500,使得 第一遍將有200個,第二個500,第三個347,然後192 在最後通過?

In [10]: df.join(pd.DataFrame([[1]*x for x in df['months']], 
    ...:      columns=['{}/1/2016'.format(i) for i in range(1,13)], 
    ...:      index=df.index).fillna(0).mul(df['allot'], axis=0)) 
    ...: 
    ...: 
Out[10]: 
    allot months 1/1/2016 2/1/2016 3/1/2016 4/1/2016 5/1/2016 6/1/2016 7/1/2016 8/1/2016 9/1/2016 10/1/2016 11/1/2016 \ 
0 200  12  200.0  200.0  200.0  200.0  200.0  200.0  200.0  200.0  200.0  200.0  200.0 
1 500  10  500.0  500.0  500.0  500.0  500.0  500.0  500.0  500.0  500.0  500.0  0.0 
2 347  6  347.0  347.0  347.0  347.0  347.0  347.0  0.0  0.0  0.0  0.0  0.0 
3 192  7  192.0  192.0  192.0  192.0  192.0  192.0  192.0  0.0  0.0  0.0  0.0 

    12/1/2016 
0  200.0 
1  0.0 
2  0.0 
3  0.0 
+0

這真的很接近!說雖然我有這個DF。 'df = pd.DataFrame({'months':[12,10,6,7],'allot':[200,500,347,192]})'。我如何用df ['allot']行中的值替換500的值,以便第一次傳遞將在最後一次傳遞中具有200,第二個500,第三個347和然後是192。無論哪種方式,感謝您的建議。我會盡力與此運行,並繼續堵塞:) – mnickey

+0

@mnickey,檢查更新... – MaxU

+0

輝煌,謝謝! – mnickey