添加具有不同标记的列会导致NaN

时间:2017-02-08 18:18:27

标签: python pandas nan

我的df:

   df = pd.DataFrame({'p1_profit': {0: -196.50000000000023,
      1: -593.73000000000025,
      2: -712.46000000000026,
      3: -657.88000000000011,
      4: -763.18000000000029},
     'p2_profit': {0: -634.36999999999989,
      1: -737.14999999999998,
      2:  224.41999999999985,
      3: -697.20000000000005,
      4: -526.78999999999996},
     'p3_profit': {0: 211.32999999999981,
      1: -155.02000000000021,
      2: 443.90999999999985,
      3: -75.320000000000164,
      4: 276.24999999999989}})

我希望将每行的列中的可盈利年数加起来,其中p1_profit表示年1的利润,依此类推。

我尝试过这样的事情:

np.sign(df[df.p1_profit > 0].p1_profit) + \
np.sign(df[df.p2_profit > 0].p2_profit) + \
np.sign(df[df.p3_profit > 0].p3_profit)

结果:

0   NaN
2   NaN
4   NaN
dtype: float64

问题是每个np.sign(X)的结果都有自己的索引,可能与其他np.sign(X)的调用结果不同。

期望的结果应该是row 01年有利可图的地方,row 10-年有利可图等等。

row 0: 1
row 1: 0
row 2: 2
row 3: 0
row 4: 1

1 个答案:

答案 0 :(得分:2)

在布尔结果上使用In [7]: (df > 0).sum(axis=1) Out[7]: 0 1 1 0 2 2 3 0 4 1 dtype: int64

Calendar calendar = Calendar.getInstance();
 calendar.set(Calendar.HOUR_OF_DAY, 18);
 calendar.set(Calendar.MINUTE, 30);
 calendar.set(Calendar.SECOND, 0);
 Intent intent1 = new Intent(MainActivity.this, AlarmReceiver.class);
 PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0,intent1, PendingIntent.FLAG_UPDATE_CURRENT);
 AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE);
 am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);