0

Say I have a dataframe that looks something like this

Line VarA VarB 345 Something abc 346 Something2 def 347 Something ghi 348 Something2 jkl 349 Something mno 350 Something2 pqr 351 Something stu 352 Something2 vwx 

How can I turn this dataframe into something that looks like this:

VarA VarB Something abcghimnostu Something2 defjklpqrvwx 
1
  • Please show what you have tried Commented Oct 5, 2016 at 4:08

1 Answer 1

2

You can just groupby and sum

print(df.groupby('VarA')['VarB'].sum()) VarA something abcghimnostu something2 defjklpqrvwx Name: VarB, dtype: object 
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, also how might I add spaces to each, like so "abc ghi mno stu" -- I will accept you when I can
You can add spaces in several different ways, depending what your final desired output is. You should probably post an other question about this with a little more details on the output you need.
I ended up doing for i,d in df.iterrows(): \n d['VarA'] = d['VarA'] + ' '
@MikeJohnsonJr You can also do this, df.groupby('VarA')['VarB'].apply(" ".join).reset_index()` as it was suggested by @jezrael earlier.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.