Swap rows and columns of a two-dimensional array of Python list type

Money and Business

The standard Python list type can represent a two-dimensional array by a list of lists.

This section explains how to swap the rows and columns of this two-dimensional array.

    1. Convert to NumPy array
    2. .TTranspose with this.
    1. pandas.DataFrameConvert to this
    2. .TTranspose with this.
  • Transposition with built-in function zip()

It is easier to use NumPy or pandas, but if you do not want to import NumPy or pandas just for transposition, you can use the zip() function to transpose.

The original two-dimensional array is defined as follows

import numpy as np
import pandas as pd

l_2d = [[0, 1, 2], [3, 4, 5]]

Converted to NumPy array ndarray and transposed with .T

Generate a NumPy array ndarray from the original two-dimensional array and get the transposed object with the .T attribute.

If you want a Python list-type object in the end, further convert it to a list with the tolist() method.

arr_t = np.array(l_2d).T

print(arr_t)
print(type(arr_t))
# [[0 3]
#  [1 4]
#  [2 5]]
# <class 'numpy.ndarray'>

l_2d_t = np.array(l_2d).T.tolist()

print(l_2d_t)
print(type(l_2d_t))
# [[0, 3], [1, 4], [2, 5]]
# <class 'list'>

In addition to the .T attribute, the ndarray method transpose() and the function numpy.transpose() can also be used.

Converted to pandas.DataFrame and transposed with .T

Generate a pandas.DataFrame from the original two-dimensional array and get the transposed object with the .T attribute.

If you want a Python list-type object in the end, get numpy.ndarray with the values attribute, and then convert it to a list with the tolist() method.

df_t = pd.DataFrame(l_2d).T

print(df_t)
print(type(df_t))
#    0  1
# 0  0  3
# 1  1  4
# 2  2  5
# <class 'pandas.core.frame.DataFrame'>

l_2d_t = pd.DataFrame(l_2d).T.values.tolist()

print(l_2d_t)
print(type(l_2d_t))
# [[0, 3], [1, 4], [2, 5]]
# <class 'list'>

Transposition with built-in function zip()

Transposes a two-dimensional array using the built-in function zip().

zip() is a function that returns an iterator that summarizes the elements of multiple iterables (lists, tuples, etc.). It is used when running multiple lists in a for loop, for example.

In addition, the function uses a mechanism whereby the list can be expanded and passed if the function argument is marked with an asterisk.

Transpositions can be made as follows.

l_2d_t_tuple = list(zip(*l_2d))

print(l_2d_t_tuple)
print(type(l_2d_t_tuple))
# [(0, 3), (1, 4), (2, 5)]
# <class 'list'>

print(l_2d_t_tuple[0])
print(type(l_2d_t_tuple[0]))
# (0, 3)
# <class 'tuple'>

As it is, the elements inside are tuples. Therefore, if you want to make it a list, use list(), which converts a tuple to a list in list comprehension notation.

l_2d_t = [list(x) for x in zip(*l_2d)]

print(l_2d_t)
print(type(l_2d_t))
# [[0, 3], [1, 4], [2, 5]]
# <class 'list'>

print(l_2d_t[0])
print(type(l_2d_t[0]))
# [0, 3]
# <class 'list'>

The following is a step-by-step breakdown of the process.

The elements of the list are expanded with an asterisk, the expanded elements are grouped together with the zip() function, and then the tuple is converted to a list with list comprehension notation.

print(*l_2d)
# [0, 1, 2] [3, 4, 5]

print(list(zip([0, 1, 2], [3, 4, 5])))
# [(0, 3), (1, 4), (2, 5)]

print([list(x) for x in [(0, 3), (1, 4), (2, 5)]])
# [[0, 3], [1, 4], [2, 5]]