Home > Mobile >  assigning dataframe filter result to variable
assigning dataframe filter result to variable

Time:10-13

I am trying to make calculations based on the cell values after a filter is applied. I have never encountered this problem before and suddenly it seems an issue and I do not know that I have to do.

transacties_klant DataFrame:

    crm_ref year quarter LitersSold
3   V000095 20  1.0 60.00
4   V000095 20  3.0 180.00
5   V000095 21  2.0 360.00
6   V000095 21  3.0 11.20
7   V000095 21  4.0 649.04
8   V000095 22  1.0 1107.00
9   V000095 22  2.0 3100.80
10  V000095 22  3.0 2164.20

The output of this code should be the variable containing an float but instead it returns a? Series, dataframe, multi-index?

last_quarter_year = 22
last_quarter = 2
last_quarter_sum = transacties_klant[(transacties_klant['year']==last_quarter_year) & (transacties_klant['quarter']==last_quarter)]['LitersSold']
quarter_before_sum = transacties_klant[(transacties_klant['year']==(last_quarter_year-1)) & (transacties_klant['quarter']==last_quarter)]['LitersSold']
print(last_quarter_sum)

However the current output is like this:

9    3100.8
Name: LitersSold, dtype: float64

I have already tried the .astype() and .values(). What do I need to change so last_quarter_sum = 3100.8 ?

CodePudding user response:

you need to extract the value from the result set

last_quarter_sum.values[0]

CodePudding user response:

Use squeeze:

last_quarter_sum = (transacties_klant
                   .loc[(transacties_klant['year']==last_quarter_year)
                      & (transacties_klant['quarter']==last_quarter),
                       'LitersSold'].squeeze()
                    )

CodePudding user response:

transacties_klant[(transacties_klant['year']==last_quarter_year) & (transacties_klant['quarter']==last_quarter)] created a dataframe holding only the rows you selected. Adding ['LitersSold'] after that selects a single column of the dataframe expressed as as Series object. That Series could have zero or more entries. If you want the sum of all matching rows, use .sum() to complete.

last_quarter_sum = transacties_klant[(transacties_klant['year']==last_quarter_year) & (transacties_klant['quarter']==last_quarter)]['LitersSold'].sum()
  • Related