2

I've a dataframe df as follows such that it's the number of columns vary time to time:

  Column 1     Column 2   
0        A        E  
1        B        F  
2        C        G  
3        D        H  

I need to combine the columns into one such that it looks like as given below:

        column 3  
0         A,E  
1         B,F  
2         C,G  
3         D,H  

For rows to combine into one the following is the code. I need something like that which will not specify the column names.

df = df.stack().to_frame().T  
df.columns = ['{}_{}'.format(*c) for c in df.columns] 

2 Answers 2

3
In [52]: df.Column1 += ',' + df.pop('Column2')

In [53]: df
Out[53]:
  Column1
0     A,E
1     B,F
2     C,G
3     D,H

or

In [56]: df['Column3'] = df.pop('Column1') + ',' + df.pop('Column2')

In [57]: df
Out[57]:
  Column3
0     A,E
1     B,F
2     C,G
3     D,H

UPDATE:

In [77]: df
Out[77]:
  Column1 Column2 Column3
0       A       E       I
1       B       F       J
2       C       G       K
3       D       H       L

In [78]: res = pd.DataFrame({'ColX': df.add(',').sum(axis=1).str.rstrip(',')}, df.index)

In [79]: res
Out[79]:
    ColX
0  A,E,I
1  B,F,J
2  C,G,K
3  D,H,L
Sign up to request clarification or add additional context in comments.

1 Comment

Is there any way of not specifying the column names? Because the number columns vary time to time. I've edited my question,please refer it.
1

Here's one with .join -

pd.DataFrame({'Out':[",".join(i) for i in df.values]})

Sample run -

In [375]: df
Out[375]: 
  Column1 Column2 Column3
0       A       E       I
1       B       F       J
2       C       G       K
3       D       H       L

In [376]: pd.DataFrame({'Out':[",".join(i) for i in df.values]})
Out[376]: 
     Out
0  A,E,I
1  B,F,J
2  C,G,K
3  D,H,L

1 Comment

@NayanaMadhu Good to hear that :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.