Cannot set a DataFrame with multiple columns to single column

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
4 min

banner

# Cannot set a DataFrame with multiple columns to single column

The Pandas "ValueError: Cannot set a DataFrame with multiple columns to the single column X" occurs when you try to set a DataFrame that contains multiple columns to a single column.

To solve the error, make sure the number of new columns you are adding to the DataFrame matches the number of columns on the right-hand side of the assignment.

Here is an example of how the error occurs.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'salary': [175.1, 180.2, 190.3], 'date': ['2023-01-05', '2023-03-25', '2021-01-24'] }) df2 = pd.DataFrame() # ⛔️ ValueError: Cannot set a DataFrame with multiple columns to the single column A df2['A'] = df[['name', 'salary']]

value error cannot set dataframe with multiple columns to single column

We got the error because we tried to assign the name and salary columns of df to a single column in df2.

The number of columns on the left-hand side and the number of columns on the right-hand side has to match.

If you print the DataFrame on the right-hand side, you will see that it contains 2 columns.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'salary': [175.1, 180.2, 190.3], 'date': ['2023-01-05', '2023-03-25', '2021-01-24'] }) # name salary # 0 Alice 175.1 # 1 Bobby 180.2 # 2 Carl 190.3 print(df[['name', 'salary']])

So a DataFrame that contains 2 (or more) columns, can't fit into a single column.

One way to solve the error is to make sure the specified columns on the left are exactly as many as the selected columns on the right.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'salary': [175.1, 180.2, 190.3], 'date': ['2023-01-05', '2023-03-25', '2021-01-24'] }) df2 = pd.DataFrame() df2[['A', 'B']] = df[['name', 'salary']] # A B # 0 Alice 175.1 # 1 Bobby 180.2 # 2 Carl 190.3 print(df2)

make sure number of columns on left matches

The code for this article is available on GitHub

You can also solve the error by adding the multiple columns in multiple separate assignments.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'salary': [175.1, 180.2, 190.3], 'date': ['2023-01-05', '2023-03-25', '2021-01-24'] }) df2 = pd.DataFrame() df2['A'] = df['name'] df2['B'] = df['salary'] # A B # 0 Alice 175.1 # 1 Bobby 180.2 # 2 Carl 190.3 print(df2)

add multiple columns in separate assignments

The code for this article is available on GitHub

# If you have an iterable on the right-hand side, use unpacking

If you have an iterable on values on the right-hand side (e.g. a list), use unpacking.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'salary': [175.1, 180.2, 190.3], 'date': ['2023-01-05', '2023-03-25', '2021-01-24'] }) df2 = pd.DataFrame() df2['A'], df2['B'], df2['C'] = [df['name'], df['salary'], df['date']] # A B C # 0 Alice 175.1 2023-01-05 # 1 Bobby 180.2 2023-03-25 # 2 Carl 190.3 2021-01-24 print(df2)

using unpacking to solve the error

The code for this article is available on GitHub

You have to make sure the assignments on the left are exactly as many as the values in the iterable on the right.

The list on the right-hand side contains 3 elements, so we assigned its values to 3 DataFrame columns.

# Using DataFrame.assign to solve the error

Depending on how you got the error, you might also be able to use the DataFrame.assign() method.

main.py
import pandas as pd df = pd.DataFrame() emp_names = ['Alice', 'Bobby', 'Carl'] emp_salaries = [175.1, 180.2, 190.3] df = df.assign(names=emp_names, salaies=emp_salaries) # names salaies # 0 Alice 175.1 # 1 Bobby 180.2 # 2 Carl 190.3 print(df)
The code for this article is available on GitHub

The DataFrame.assign() method is used to assign new columns to the DataFrame.

The method returns a new object with all original columns, in addition to the specified new columns.

The assign() method takes keyword arguments where the keywords are the column names.

# Using the DataFrame.insert() method to solve the error

You can also use the DataFrame.insert() method to solve the error.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'salary': [175.1, 180.2, 190.3], 'date': ['2023-01-05', '2023-03-25', '2021-01-24'] }) df.insert(0, 'ID', ['X', 'Y', 'Z']) print(df) print('-' * 50) df.insert(len(df), 'country', ['Austria', 'Belgium', 'Canada']) print(df)

Running the code sample produces the following output.

shell
ID name salary date 0 X Alice 175.1 2023-01-05 1 Y Bobby 180.2 2023-03-25 2 Z Carl 190.3 2021-01-24 -------------------------------------------------- ID name salary country date 0 X Alice 175.1 Austria 2023-01-05 1 Y Bobby 180.2 Belgium 2023-03-25 2 Z Carl 190.3 Canada 2021-01-24

using insert method to solve the error

The code for this article is available on GitHub

The DataFrame.insert() method inserts a column into a DataFrame at a specified location.

The first argument the method takes is the insertion index.

Indices are zero-based, so the specified index has to be greater than or equal to 0 and less than or equal to len(columns).

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.