Calculate partition columns with multiply /divide

Hi,
I want to partition dataframe and then combine to find multiples or divisions of columns in partition.

My example such: to calculate area sums (column square), price sums and get price per square.
I was unable to do this using combine despite combine would be preferred method.
Get some success in more complicated way, but can not calculate number of rows.
Any solutions ?

Thanks,
Edvard

# Sample DataFrame
df = DataFrame(
    Size = ["A", "B", "A", "B"],
    Price = [1000000, 2000000, 3000000, 4000000],
    Square = [100, 200, 150, 250]
)

# Group by "Size"
partitions_by_size = groupby(df, :Size)

# Define a custom function to calculate PriceM2 within a partition
function calculate_priceM2(partition)
    return sum(partition.Price) / sum(partition.Square)
end

# Calculate "Kaina_sum_mln" and "KainaM2" within each partition using the custom function

data_by_size = DataFrame(
    Size = unique(df.Size),
 #   nrow = nrow(partitions_by_size),
 #   nrow => count,
    Price_sum_mln = combine(partitions_by_size, :Price => (price -> sum(price) / 1000000)),
    PriceM2 = [calculate_priceM2(partition) for partition in partitions_by_size]
)

try this

pbs = groupby(df, :Size)

transform(combine(pbs,[2,3].=>sum), [2,3]=>ByRow(/))

or this


combine(pbs,[2,3]=>(x,y)->sum(x)/sum(y))

Hi, rocco_sprmnt21,

I tried both solutions and both works :slight_smile:

I think as will use

combine(pbs,[2,3]=>(x,y)->sum(x)/sum(y))

as its syntax more clear for me.

Thanks for advice !

Edvard

I’m glad it was useful to you.
This is a different solution along the lines of the one you were looking for

pbs = groupby(df, :Size)
pfsm(p,sm)=sum(p)/sum(sm)
combine(pbs,[2,3]=>pfsm=>:price_for_sm)