merging all columns with numerical values in to a single column

Viewed 18

I have the following Pandas data frame:

A1   A2  A3......... A10
2    32  41.........  11
12   44  21.........  21
10   3   90.........  80  

And I am trying to create a pandas data frame with one single column with values of all other columns separated by a single space. The type of this resulting column should be an object. it should look like this:

        A
2 32 41.........  11
12 44 21.........  21
10 3  90.........  80

I have tried the following code; however, that requires me to convert the datatype for each column to be a string which I do not want to do:

df['A1'] + ' ' + df['A2']+ ' ' + df['A3'] + ' ' + df['A4']+......+df['A10'].astype(str)
1 Answers

It is impossible join numbers like strings without converting to strings in python.

Only is possible create lists:

#all columns with A in column name
df['A'] = df.filter(like='A').agg(list, axis=1)

#all columns
df['A'] = df.agg(list, axis=1)
Related