0

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

1 Answer 1

3
  • 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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.