Error creating columns using numpy and pandas

import numpy as np
import pandas as pd
df = pd.DataFrame({
‘Name’:[‘Gullu’,‘Pinto’,‘Dan’,‘Billo’, np.nan],
‘Age’:[23,32,12,32,34,13],
‘Job’:[‘Painter’,‘Singer’,np.nan,np.nan,‘Coder’,np.nan],
‘City’:[np.nan, ‘Paris’, np.nan, np.nan, ‘Tokyo’, np.nan],
‘Zipcode’: [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan]},
columns=[‘Name’, ‘Age’,‘Job’,‘City’,‘Zipcode’])

print(df)

Traceback (most recent call last):
File “./remove_columns_pandas.py”, line 48, in
columns=[‘Name’, ‘Age’,‘Job’,‘City’,‘Zipcode’])
File “/usr/lib/python3/dist-packages/pandas/core/frame.py”, line 330, in init
mgr = self._init_dict(data, index, columns, dtype=dtype)
File “/usr/lib/python3/dist-packages/pandas/core/frame.py”, line 419, in _init_dict
extract_index(list(data.values()))
File “/usr/lib/python3/dist-packages/pandas/core/frame.py”, line 6211, in extract_index
raise ValueError(‘arrays must all be same length’)
ValueError: arrays must all be same length

Hi,

Is your question about why you’re receiving the error?

The error message says “arrays must all be the same length” at the end. Typically, this means you’re trying to create a DataFrame where your columns have different lengths. And if you look closely, you can see that Name only has 5 items (Gullu, Pinto, Dan, Billo, and NaN), while the others all have 6.

How to fix this will depend on what this code is supposed to be doing. My guess is that you forgot an item in Name. If you’d like to come back with more context, we can help you more easily.

P.S., you can format code more readably by using triple backticks, i.e., ``` on a line before and after your code.

1 Like

yes my question is about why I am receiving the error. I am new to the forum. I will use triple backticks in the future to format my code to make it more readable. I did see that i forgot an item in Name. Thank you for pointing that out. I appreciate it.

1 Like