![]() |
VOOZH | about |
Given a matrix containing strings, the task is to perform vertical concatenation, where elements from each column are joined together to form a single string for that column.
Input : [["Gfg", "good"], ["is", "for"]]
Output : ['Gfgis', 'goodfor']
Explanation: Elements in the same column are concatenated "Gfg" is joined with "is", and "good" with "for", forming new strings for each column.
Let's look at the different methods to vertical concatenate in matrix in Python.
This method converts the list of lists into a DataFrame, then uses the apply() function with ''.join() to concatenate strings column-wise. Pandas handles uneven data efficiently by filling missing values automatically with empty strings.
['GfgisBest', 'goodfor']
Explanation:
This method uses NumPy to efficiently transpose and concatenate matrix columns. Shorter lists are padded with empty strings before transposing to maintain uniform column lengths.
['GfgisBest', 'goodfor']
Explanation:
This method performs vertical concatenation by pairing elements column-wise using zip_longest() and joining them with "".join(). Missing elements are automatically filled with empty strings, ensuring smooth concatenation even for uneven matrices.
['GfgisBest', 'goodfor']
Explanation:
This method manually iterates through columns and rows to concatenate elements. It handles uneven sublists using exception handling and appends the concatenated column string to the result list.
['GfgisBest', 'goodfor']
Explanation: