Last updated: Apr 12, 2024
Reading time·4 min
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.
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']]
We got the error because we tried to assign the name
and salary
columns of
df
to a single column in df2
.
If you print the DataFrame
on the right-hand side, you will see that it
contains 2 columns.
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.
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)
You can also solve the error by adding the multiple columns in multiple separate assignments.
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)
If you have an iterable on values on the right-hand side (e.g. a list), use unpacking.
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)
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.
DataFrame.assign
to solve the errorDepending on how you got the error, you might also be able to use the DataFrame.assign() method.
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 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.
DataFrame.insert()
method to solve the errorYou can also use the DataFrame.insert()
method to solve the error.
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.
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
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)
.
You can learn more about the related topics by checking out the following tutorials: