title: Shouldn't I be able to fillna column derived with transform?
body: <p>I would like to concatenate only the unique values of a string(product) aggregated by two other columns(customer and product). I am using pandas 24.2 .To deal up with the duplicated products after grouping i decided to drop them before the transformation and fill them after that. </p>
  
  <pre><code>import pandas as pd
  from random import randint
  df=pd.DataFrame({"customer":['ABC','ABC','ABC','ABC','ABC','ABC','ABC','ABC','BCF','BCF','BCF','GF','GF','GF'],
                   "period":[1,2,2,2,3,3,3,3,1,1,2,1,2,3],
                   "product":['prod1','prod1','prod1','prod2','prod2','prod1','prod1','prod2','prod1','prod2','prod2','prod1','prod1','prod1'],
                   "var":[randint(1,20) for i in range(14)]})
  
  df=df.sort_values(by=["customer","period","product"])
  </code></pre>
  
  <p>My approach was to transform with .join function. It kinda worked but I realized that i can repeat a product on each row.</p>
  
  <pre><code>df["product_mix1"]=df[["customer","period","product"]].groupby(["customer","period"])["product"].transform(lambda x: " ".join(x))
  </code></pre>
  
  <p>To deal up with the duplicated products after grouping i decided to drop them before the transformation and fill them after that. It almost worked but the fillna statement did not change the things as expected.    </p>
  
  <pre><code>df["product_mix2"]=df[["customer","period","product"]].drop_duplicates(["customer","period","product"])\
                          .groupby(["customer","period"])["product"].transform(lambda x: " ".join(x)).fillna(method='ffill')
  </code></pre>
  
  <p>On the other hand, doing it in two steps got me to the desired result. </p>
  
  <pre><code>df["product_mix3"]=df[["customer","period","product"]].drop_duplicates(["customer","period","product"])\
                          .groupby(["customer","period"])["product"].transform(lambda x: " ".join(x))
  
  df["product_mix3"]= df["product_mix3"].fillna(method="ffill")
  </code></pre>
  
  <p>I was wondering if there is a one-liner for the above derivation still using transform. I previously thought that transform aggregates series and then merges it back to the original frame. However, it seems that the fillna is processed before the final merge to the frame. Is this the expected behaviour?</p>
  
  <p>Finnally, here is the equivalent one-liner with agg+merge that is a bit uglier but does the job quite faster.</p>
  
  <pre><code>df=df.merge(df[["customer","period","product"]].drop_duplicates(["customer","period","product"])\
                          .groupby(["customer","period"])["product"].agg(lambda x: " ".join(x)).to_frame().\
                          rename(columns={"product":"product_mix4"}),\
                          how="left",on=["customer","period"])
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/4nyll.png" rel="nofollow noreferrer">image with the results after the execution of the above code</a></p>