2016-07-13 6 views
0

私は現在、特定のセルにクラス要素を適用するためにIFステートメントを作成しようとしています。 .to_html関数を使用しているので、私は現在、パンダが与えるスタイリングオプションを使用できません。Pandas Dataframe if適用エラーを使用したステートメント

私の表は次のとおりです。 { 'Pwer' [ - 3、-1.3、-2.4]、 'トレンド':[1.3、-1.3、-1.7]}

Iは

if((hist['Pwer']<0) & (hist['Trend']<0)): 
      hist['Pwer']=hist['Pwer'].apply(lambda x:'<span class="Negative Trend_neg">{0}</span>'.format(x)) 

を試みたが、それは

もたらします

hist( 'Pwer')とhist( 'Trend')の両方に.all()を適用しても、何が起こっているのか分かりません。どんな助けでも感謝します。

答えて

0

私はあなたがmaskが必要だと思う:

hist = pd.DataFrame({'Pwer':[-.3,-1.3,-2.4], 'Trend':[1.3,-1.3,-1.7]}) 
print (hist) 
    Pwer Trend 
0 -0.3 1.3 
1 -1.3 -1.3 
2 -2.4 -1.7 

hist['Pwer']=hist['Pwer'].mask((hist['Pwer']<0) & (hist['Trend']<0), 
           hist['Pwer'].apply(lambda x:'<span class="Negative Trend_neg">{0}</span>'.format(x))) 
print (hist) 
              Pwer Trend 
0           -0.3 1.3 
1 <span class="Negative Trend_neg">-1.3</span> -1.3 
2 <span class="Negative Trend_neg">-2.4</span> -1.7 
2

このような何かが動作します:

cond = ((hist['Pwer']<0) & (hist['Trend']<0)) 
hist['Pwer'][cond] = hist['Pwer'][cond].apply(lambda x:'<span class="Negative Trend_neg">{0}</span>'.format(x)) 

出力は

          Pwer Trend 
0           -0.3 1.3 
1 <span class="Negative Trend_neg">-1.3</span> -1.3 
2 <span class="Negative Trend_neg">-2.4</span> -1.7 
です
関連する問題