ValueError: All arrays must be of the same length when using pandas.DataFrame

I’m on Day 25 and lesson 228. Toward the end of the lesson, import pandas, Create a dataframe from scratch . when the instructor runs the program it comes out ok but when I run it I get an error.

ValueError: All arrays must be of the same length.
import pandas

# Create a dataframe from scratch.
import pandas

data_dict = {
    "students": ["Amy", "James", "Angela"],
    "scores": ["76, 56, 65"]
} 
data = pandas.DataFrame(data_dict)
print(data)

:slightly_smiling_face:

Is Replit 101 a good platform to go to ask questions being that is seems to be relatively new?
:slightly_smiling_face:

Yes, @QZapper. That is an excellent place to go for live support.

1 Like

Thank you @BrittanyatReplit. What about my ‘DataFrame from scratch question?’ I’ve noticed a lot of questions on askreplit that others have posted with copies of their code questions. i’m new at posting. Was my format incorrect and if so how should I be submitting copies of code? :thinking:

You can format code with ``` I did it for you this time so don’t worry about it. Some more info is in this guide. Unfortenly I don’t know pandas so I cant assist you.

You can also create an invite link and share the link here so other users can look at your code directly to help you out.

Hi @QZapper thanks for your question.

I think the issue is with the following line of code:

    "scores": ["76, 56, 65"]

You need to remove the quotes before the number 76 and after the number 65 as below:

    "scores": [76, 56, 65]

The program then works correctly. I’ve made a copy here for reference https://replit.com/@IanAtCSTeach/Day25#main.py

2 Likes

Hey @IanAtCSTeach , thank you so much for that correction. I am a rookie so I guess I shouldn’t fell as bad as I do for not catching that clumsy error. I swore I looked at everything. Thank you. :+1:

1 Like

Hi @QZapper never a problem! Thanks for the thanks but we keep learning all the time. 39 years programming and still make the odd mistake that takes me a while to see!

I’m somewhat late to this, but the reason you’re encountering that error is that the scores are provided as a single string rather than as a list of strings or integers. In a DataFrame, each key-value pair in the dictionary corresponds to a column, with the key serving as the column name and the value as the list of column values. All columns must have the same number of elements.

import pandas as pd  

data_dict = {
    "students": ["Amy", "James", "Angela"],
    "scores": [76, 56, 65]  # Provide the scores as a list of integers
} 

data = pd.DataFrame(data_dict)
print(data)
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.