Pandas error in Python: columns must be same length as key

Pandas Error In Python: Columns Must Be Same Length As Key

We Are Going To Discuss About Pandas error in Python: columns must be same length as key. So lets Start this Article.

You want a bit modify resolution, as a result of typically it return 2 and typically just one column:

df2 = pd.DataFrame({'STATUS':['Estimated 3:17 PM','Delayed 3:00 PM']})


df3 = df2['STATUS'].str.break up(n=1, broaden=True)
df3.columns = ['STATUS_ID{}'.format(x+1) for x in df3.columns]
print (df3)
  STATUS_ID1 STATUS_ID2
0  Estimated    3:17 PM
1    Delayed    3:00 PM

df2 = df2.be part of(df3)
print (df2)
              STATUS STATUS_ID1 STATUS_ID2
0  Estimated 3:17 PM  Estimated    3:17 PM
1    Delayed 3:00 PM    Delayed    3:00 PM

Another attainable knowledge – all knowledge haven’t any whitespaces and resolution working too:

df2 = pd.DataFrame({'STATUS':['Canceled','Canceled']})

and resolution return:

print (df2)
     STATUS STATUS_ID1
0  Canceled   Canceled
1  Canceled   Canceled

All collectively:

df3 = df2['STATUS'].str.break up(n=1, broaden=True)
df3.columns = ['STATUS_ID{}'.format(x+1) for x in df3.columns]
df2 = df2.be part of(df3)

To clear up this error, test the form of the item you’re making an attempt to assign the df columns (utilizing np.form). The second (or the final) dimension must match the variety of columns you’re making an attempt to assign to. For instance, when you attempt to assign a 2-column numpy array to three columns, you’ll see this error.

A normal workaround (for case 1 and case 2 under) is to forged the item you’re making an attempt to assign to a DataFrame and be part of() it to df, i.e. as a substitute of (1), use (2).

df[cols] = vals   # (1)
df = df.be part of(vals) if isinstance(vals, pd.DataFrame) else df.be part of(pd.DataFrame(vals))  # (2)

If you’re making an attempt to exchange values in an present column and bought this error (case 3(a) under), convert the item to listing and assign.

df[cols] = vals.values.tolist()

If you might have duplicate columns (case 3(b) under), then there’s no simple repair. You’ll should make the size match manually.

READ :  BeautifulSoup: How to get nested divs


This error happens in 3 instances:

Case 1: When you attempt to assign a list-like object (e.g. lists, tuples, units, numpy arrays, and pandas Series) to a listing of DataFrame column(s) as new arrays1 however the variety of columns doesn’t match the second (or final) dimension (discovered utilizing np.form) of the list-like object. So the next reproduces this error:

df = pd.DataFrame({'A': [0, 1]})
cols, vals = ['B'], [[2], [4, 5]]
df[cols] = vals # variety of columns is 1 however the listing has form (2,)

Note that if the columns are usually not given as listing, pandas Series, numpy array or Pandas Index, this error gained’t happen. So the next doesn’t reproduce the error:

df[('B',)] = vals # the column is given as a tuple

One attention-grabbing edge case happens when the list-like object is multi-dimensional (however not a numpy array). In that case, below the hood, the item is forged to a pandas DataFrame first and is checked if its final dimension matches the variety of columns. This produces the next attention-grabbing case:

# the error happens under as a result of pd.DataFrame(vals1) has form (2, 2) and len(['B']) != 2
vals1 = [[[2], [3]], [[4], [5]]]
df[cols] = vals1

# no error under as a result of pd.DataFrame(vals2) has form (2, 1) and len(['B']) == 1
vals2 = [[[[2], [3]]], [[[4], [5]]]]
df[cols] = vals2

Case 2: When you attempt to assign a DataFrame to a listing (or pandas Series or numpy array or pandas Index) of columns however the respective numbers of columns don’t match. This case is what prompted the error in the OP. The following reproduce the error:

df = pd.DataFrame({'A': [0, 1]})
df[['B']] = pd.DataFrame([[2, 3], [4]]) # a 2-column df is making an attempt to be assigned to a single column

df[['B', 'C']] = pd.DataFrame([[2], [4]]) # a single column df is making an attempt to be assigned to 2 columns

Case 3: When you attempt to exchange the values of present column(s) by a DataFrame (or a list-like object) whose variety of columns doesn’t match the variety of columns it’s changing. So the next reproduce the error:

# case 3(a)
df1 = pd.DataFrame({'A': [0, 1]})
df1['A'] = pd.DataFrame([[2, 3], [4, 5]]) # df1 has a single column named 'A' however a 2-column-df is making an attempt to be assigned

# case 3(b): duplicate column names matter too
df2 = pd.DataFrame([[0, 1], [2, 3]], columns=['A','A'])
df2['A'] = pd.DataFrame([[2], [4]]) # df2 has 2 columns named 'A' however a single column df is being assigned

1: df.loc[:, cols] = vals might overwrite knowledge inplace, so this gained’t produce the error however will create columns of NaN values.

READ :  [Solved] Ubuntu16.4Install vcs2016 Error: mount.vboxsf: mounting failed with the error: Invalid argument

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Leave a Reply

Your email address will not be published. Required fields are marked *