Reputation: 537
Can someone explain to me what is the cause of this error?
import pandas as pd
df = pd.DataFrame([
[11, 2, 5, 3],
[4, 35, 7, 36],
[4, 5, 17, 16],
[14, 5, 37, 6]
],
index=["col1"],
columns=["col1", "col2", "col3", "col4"]
)
Upvotes: 1
Views: 6312
Reputation:
The issue is the index=['col1']
argument. The data you're passing to pd.DataFrame()
is a list of 4 lists, where each list has 4 items, so there will be 4 rows and 4 columns.
But, you're setting the index (row labels) to be only 1 item, when there are going to be 4 items. You need to either reduce the data to 1 row, or add 3 more items to your index=['col1']
list:
import pandas as pd
df = pd.DataFrame([
[11, 2, 5, 3],
[4, 35, 7, 36],
[4, 5, 17, 16],
[14, 5, 37, 6]
],
index=["col1", "col2", "col3", "col4"], # <--- more items added
columns=["col1", "col2", "col3", "col4"]
)
Output:
>>> df
col1 col2 col3 col4
col1 11 2 5 3
col2 4 35 7 36
col3 4 5 17 16
col4 14 5 37 6
Upvotes: 2