Pandas DataFrame Colon Concatenation

I have a pandas Dataframe y with 1 million rows and 5 columns.

np.shape(y) (1037889, 5) 

The column values ​​are all 0 or 1. The expression looks something like this:

 y.head() a, b, c, d, e 0, 0, 1, 0, 0 1, 0, 0, 1, 1 0, 1, 1, 1, 1 0, 0, 0, 0, 0 

I want a Dataframe with 1 million rows and 1 column.

 np.shape(y) (1037889, ) 

where a column is only 5 columns combined together.

 New column 0, 0, 1, 0, 0 1, 0, 0, 1, 1 0, 1, 1, 1, 1 0, 0, 0, 0, 0 

I keep trying different things like merge , concat , dstack , etc .... but can't figure it out.

+7
python merge numpy pandas concatenation
source share
1 answer

If you want all the data in a new column to be concatenated into a row, this is a good case for the apply () function:

 >>> df = pd.DataFrame({'a':[0,1,0,0], 'b':[0,0,1,0], 'c':[1,0,1,0], 'd':[0,1,1,0], 'c':[0,1,1,0]}) >>> df abcd 0 0 0 0 0 1 1 0 1 1 2 0 1 1 1 3 0 0 0 0 >>> df2 = df.apply(lambda row: ','.join(map(str, row)), axis=1) >>> df2 0 0,0,0,0 1 1,0,1,1 2 0,1,1,1 3 0,0,0,0 
+10
source share

All Articles