eli
eli

Reputation: 35

Merge row data of python data frame based on some condition

I have a python data from as below. in every row I have some similar "string", in every round of similar string, I want to merge the first one with the first, then the first one with second one, then the first one with the third, can anyone help me please?

   row   label
1  x     a
2  x     bb
3  x     cc
4  x     rr
5  x     uu
6  y     ff
7  y     bb
8  y     nn

I want :

0  x  a
1  x  abb
2  x  acc
3  x  arr
4  x  auu
5  y  ff 
6  y  ffbb
7  y  ffnn

Upvotes: 0

Views: 56

Answers (1)

Rob Raymond
Rob Raymond

Reputation: 31236

  • your transform is append first item in series to all other item in series
  • this is use of transform() with a lambda that does a list comprehension
df = pd.read_csv(io.StringIO("""   row   label
1  x     a
2  x     bb
3  x     cc
4  x     rr
5  x     uu
6  y     ff
7  y     bb
8  y     nn"""), sep="\s+")

df["label"] = df.groupby("row")["label"].transform(lambda s: [s.values[0]+x if i>0 else x for i,x in enumerate(s)])

row label
1 x a
2 x abb
3 x acc
4 x arr
5 x auu
6 y ff
7 y ffbb
8 y ffnn

Upvotes: 3

Related Questions