0

I have the following df:

d = {'col1': [0.1, 0.2, 1.2]}
dataframe = pd.DataFrame(data=d)

I want to sum up every value smaller than 1. As I get as a result 0.3 in this case. How to do it ideally?

Adler Müller
  • 218
  • 1
  • 9

2 Answers2

2

Try this:

dataframe.loc[dataframe['col1'] < 1, 'col1'].sum()

Or:

dataframe.mask(dataframe['col1'] > 1).sum()['col1']
U12-Forward
  • 65,118
  • 12
  • 70
  • 89
1

You can filter out the higher than 1 values and sum:

dataframe.where(dataframe['col1'].lt(1))['col1'].sum()
mozway
  • 81,317
  • 8
  • 19
  • 49