Compare Headers of Two pandas DataFrames in Python (3 Examples) | intersection & difference Function

Published: 29 June 2022
on channel: Statistics Globe
245
4

How to compare the headers of two pandas DataFrames in the Python programming language. More details: https://statisticsglobe.com/compare-h...
Python code of this video:

import pandas as pd # Import pandas library

data1 = pd.DataFrame({'x1':range(0, 6), # Create first pandas DataFrame
'x2':['a', 'b', 'c', 'd', 'e', 'f'],
'x3':range(10, 16),
'x4':['a', 'a', 'a', 'b', 'b', 'b'],
'x5':range(100, 106)})
print(data1) # Print first pandas DataFrame

data2 = pd.DataFrame({'x1':range(25, 30), # Create second pandas DataFrame
'x2':['x', 'y', 'z', 'z', 'x'],
'x5':range(30, 25, - 1),
'x6':range(50, 45, - 1)})
print(data2) # Print second pandas DataFrame

print(data1.columns.intersection(data2.columns)) # Apply intersection function
Index(['x1', 'x2', 'x5'], dtype='object')

print(data1.columns.difference(data2.columns)) # Apply difference function
Index(['x3', 'x4'], dtype='object')

print(data2.columns.difference(data1.columns)) # Apply difference function
Index(['x6'], dtype='object')

Follow me on Social Media:
Facebook – Statistics Globe Page:   / statisticsglobecom  
Facebook – Group for Discussions & Questions:   / statisticsglobe  
LinkedIn – Statistics Globe Page:   / statisticsglobe  
LinkedIn – Group for Discussions & Questions:   / 12555223  
Twitter:   / joachimschork  

Music by bensound.com


On this page of the site you can watch the video online Compare Headers of Two pandas DataFrames in Python (3 Examples) | intersection & difference Function with a duration of hours minute second in good quality, which was uploaded by the user Statistics Globe 29 June 2022, share the link with friends and acquaintances, this video has already been watched 245 times on youtube and it was liked by 4 viewers. Enjoy your viewing!