Can someone explain to me what is the cause of this error?
JavaScript
x
11
11
1
import pandas as pd
2
df = pd.DataFrame([
3
[11, 2, 5, 3],
4
[4, 35, 7, 36],
5
[4, 5, 17, 16],
6
[14, 5, 37, 6]
7
],
8
index=["col1"],
9
columns=["col1", "col2", "col3", "col4"]
10
)
11
Advertisement
Answer
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:
JavaScript
1
11
11
1
import pandas as pd
2
df = pd.DataFrame([
3
[11, 2, 5, 3],
4
[4, 35, 7, 36],
5
[4, 5, 17, 16],
6
[14, 5, 37, 6]
7
],
8
index=["col1", "col2", "col3", "col4"], # <--- more items added
9
columns=["col1", "col2", "col3", "col4"]
10
)
11
Output:
JavaScript
1
7
1
>>> df
2
col1 col2 col3 col4
3
col1 11 2 5 3
4
col2 4 35 7 36
5
col3 4 5 17 16
6
col4 14 5 37 6
7