Python – Pandas merge TypeError: object of type ‘NoneType’ has no len()

pandaspython

I'm experimenting with pandas merge left_on and right_on params.
According to Documentation 1 and Documentation 2

Documentation 1: states that left_on and right_on are field names to join on in left/right DataFrame.
Documentation 2: Columns from the left DataFrame to use as keys

What does keys means?

Following documentation 1:

left_frame = pd.DataFrame({'key': range(5),
                       'left_value': ['a', 'b', 'c', 'd', 'e']})
right_frame = pd.DataFrame({'key': range(2,7),
                       'right_value': ['f', 'g', 'h', 'i', 'j']})

I did this:

df = pd.merge(left_frame,right_frame,how='right',right_on='key')

left_frame has 'key' as field name, but yet it returns

TypeError: object of type 'NoneType' has no len()

Best Answer

It seems you need:

df = pd.merge(left_frame, right_frame, how='right', on='key')

because same left and right column names.

If columns names are different:

df = pd.merge(left_frame, right_frame, how='right', right_on='key1', left_on='key2')

What does keys means?

If check merge:

on : label or list

Field names to join on. Must be found in both DataFrames. If on is None and not merging on indexes, then it merges on the intersection of the columns by default.

And it means values in columns to join on.

Related Topic