0

Suppose that I have DataFrames df and df2. df2 may or may not have already been merged with df via

df = df.merge(df2,how='left',left_index=True,right_index=True)

When unmerged, they should have no column names in common.

What is the cleanest way to check if df and df2 have already been merged?

Brian
  • 2,163
  • 1
  • 14
  • 26
LogCapy
  • 447
  • 7
  • 20

2 Answers2

1

Combine Index.intersection and Index.empty to determine if there is are columns in common:

df.columns.intersection(df2.columns).empty

Returning True indicates that there are no columns in common.

root
  • 32,715
  • 6
  • 74
  • 87
0

Taking advantage of the fact that:

When unmerged, they should have no column names in common.

Check if any of the column names in df2 exist in df. To do so, you can utilize the "columns" property of the DataFrame.

For example:

# Create DataFrames with respectively unique columns
df = pd.DataFrame([1,2,3], columns=['a'])
df2 = pd.DataFrame([4,5,6], columns=['b'])

# False; the DataFrames have not been mereged 
not df.columns.intersection(df2.columns).empty

# Merge
df = df.merge(df2,how='left',left_index=True,right_index=True)

# True; the DataFrames have been merged
not df.columns.intersection(df2.columns).empty

Update: Suggestion comes from the comments. See similar options here: python: check if an numpy array contains any element of another array

Jacob Beauchamp
  • 532
  • 5
  • 18