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
  • 248
  • 1
  • 14

2 Answers2

2

Try this:

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

Or:

dataframe.mask(dataframe['col1'] > 1).sum()['col1']
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
1

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

dataframe.where(dataframe['col1'].lt(1))['col1'].sum()
mozway
  • 194,879
  • 13
  • 39
  • 75