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
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With