Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: Getting "TypeError: only integer scalar arrays can be converted to a scalar index" while trying to merge data frames

After renaming a DataFrame's column(s), I get an error when merging on the new column(s):

import pandas as pd

df1 = pd.DataFrame({'a': [1, 2]})
df2 = pd.DataFrame({'b': [3, 1]})

df1.columns = [['b']]

df1.merge(df2, on='b')

TypeError: only integer scalar arrays can be converted to a scalar index

like image 971
Ilyas Avatar asked Jan 23 '20 13:01

Ilyas


People also ask

How can you resolve TypeError only integer scalar arrays can be converted to a scalar index?

Error encountered is: "TypeError: only integer scalar arrays can be converted to a scalar index". This error comes when we handle Numpy array in the wrong way using Numpy utility functions. The fix is to refer the documentation of the Numpy function and use the function correctly.

What is scalar values in pandas?

Scalars are single values representing one unit of data, such as an integer or bool , as opposed to data structures like a list or tuple , which are composed of scalars.

Can we create Dataframe from scalar values?

The most common ways of creating data frames in Python are using lists and dictionaries. You can use a list of lists or a dictionary of lists.


Video Answer


1 Answers

When renaming columns, use DataFrame.columns = [list], not DataFrame.columns = [[list]]:

df1 = pd.DataFrame({'a': [1, 2]})
df2 = pd.DataFrame({'b': [3, 1]})

df1.columns = ['b']

df1.merge(df2, on='b')
#   b
# 0 1
like image 102
Max Ghenis Avatar answered Sep 28 '22 00:09

Max Ghenis