1

How can I achieve the expected result from the following DataFrame

 df
            col_1             col_2    col_3
     0  Non-Saved    www.google.com   20,567
     1             www.facebook.com      
     2             www.linkedin.com      
     3      Saved     www.Quora.com    6,337
     4                www.gmail.com      

Expected result:

            col_1              col_2    col_3
     0  Non-Saved     www.google.com   20,567
                    www.facebook.com
                    www.linkedin.com
     1  Saved          www.Quora.com    6,337
                       www.gmail.com   

From 5 rows to 2 rows by merging the empty strings in col_1 and col_3. Also, concatenating values in col_2 into one cell. Can anyone help me with an user-defined function to do this?

1 Answer 1

2

Let's try:

df = df.apply(lambda x: x.str.strip()).replace('',np.nan)

df.groupby(df.col_1.ffill())\
  .agg({'col_2': lambda x: ' '.join(x) ,'col_3':'first'})\
  .reset_index()

Output:

       col_1                                             col_2   col_3
0  Non-Saved  www.google.com www.facebook.com www.linkedin.com  20,567
1      Saved                       www.Quora.com www.gmail.com   6,337
Sign up to request clarification or add additional context in comments.

5 Comments

df = df.apply(lambda x: x.str.strip()).replace('',np.nan) Gives an error: AttributeError: ('Can only use .str accessor with string values, which use np.object_ dtype in pandas', u'occurred at index NumberOfRequests')
Yes, but see this SO Post
You can try '\n'.join(x)
col_1 object col_2 object col_3 int64 dtype: object
I have a slight tricky condition here -> Combine_rows.... Would you mind taking a look once. Meanwhile I am trying as well.

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.