note.nkmk.me

pandas: Transpose DataFrame (swap rows and columns)

Posted: 2019-11-21 / Tags: Python, pandas

Use the T attribute or the transpose() method to swap (= transpose) the rows and columns of pandas.DataFrame.

Neither method changes the original object, but returns a new object with the rows and columns swapped (= transposed object).

Note that depending on the data type dtype of each column, a view is created instead of a copy, and changing the value of one of the original and transposed objects will change the other.

Here, the following contents will be described.

  • pandas.DataFrame.T
  • pandas.DataFrame.transpose()
  • Change the original object itself
  • View and copy

The following data is used as an example.

import pandas as pd

df = pd.read_csv('data/src/sample_pandas_normal.csv').head(3)
print(df)
#       name  age state  point
# 0    Alice   24    NY     64
# 1      Bob   42    CA     92
# 2  Charlie   18    CA     70

Click here for sample CSV file.

See the following posts for transposing numpy.ndarray or a two-dimensional list (list of lists).

Sponsored Link

pandas.DataFrame.T

df_t = df.T
print(df_t)
#            0    1        2
# name   Alice  Bob  Charlie
# age       24   42       18
# state     NY   CA       CA
# point     64   92       70

pandas.DataFrame.transpose()

df_tr = df.transpose()
print(df_tr)
#            0    1        2
# name   Alice  Bob  Charlie
# age       24   42       18
# state     NY   CA       CA
# point     64   92       70
Sponsored Link

Change the original object itself

Parameter like inplace that change the original object itself are not provided. If you do not want to create a new object, you can assign it to the original object itself.

df = df.T
print(df)
#            0    1        2
# name   Alice  Bob  Charlie
# age       24   42       18
# state     NY   CA       CA
# point     64   92       70

View and copy

As in the previous examples, if the data type dtype is different for each column, T or transpose() generates a copy. If you change the value of one of the original and the transposed object, the other remains the same.

df = pd.read_csv('data/src/sample_pandas_normal.csv').head(3)
df_t = df.T

df.at[0, 'age'] = 100

print(df)
#       name  age state  point
# 0    Alice  100    NY     64
# 1      Bob   42    CA     92
# 2  Charlie   18    CA     70

print(df_t)
#            0    1        2
# name   Alice  Bob  Charlie
# age       24   42       18
# state     NY   CA       CA
# point     64   92       70

If all columns have the same dtype, T and transpose() by default create a view instead of a copy. Changing the value of either the original object or the transposed object also changes the other value.

df_ = pd.DataFrame(pd.np.arange(6).reshape(2, 3))
print(df_)
#    0  1  2
# 0  0  1  2
# 1  3  4  5

df_t_ = df_.T
print(df_t_)
#    0  1
# 0  0  3
# 1  1  4
# 2  2  5

df_.iat[0, 0] = 100

print(df_)
#      0  1  2
# 0  100  1  2
# 1    3  4  5

print(df_t_)
#      0  1
# 0  100  3
# 1    1  4
# 2    2  5

If you want to handle the original object and the transposed object separately, create a copy explicitly with copy().

df_ = pd.DataFrame(pd.np.arange(6).reshape(2, 3))
df_t_copy = df_.T.copy()

df_.iat[0, 0] = 100

print(df_)
#      0  1  2
# 0  100  1  2
# 1    3  4  5

print(df_t_copy)
#    0  1
# 0  0  3
# 1  1  4
# 2  2  5

In transpose(), a copy is always generated if copy is set to True.

df_ = pd.DataFrame(pd.np.arange(6).reshape(2, 3))
df_t_copy = df_.transpose(copy=True)

df_.iat[0, 0] = 100

print(df_)
#      0  1  2
# 0  100  1  2
# 1    3  4  5

print(df_t_copy)
#    0  1
# 0  0  3
# 1  1  4
# 2  2  5

The default setting is copy=False, which creates a view instead of a copy if possible. If the dtype is different for each column as in the first example, a copy is created even if it is the default (copy=False).

Sponsored Link
Share

Related Categories

Related Posts