In Python, the pandas
library includes built-in functionalities that allow you to perform different tasks with only a few lines of code. One of these functionalities is how to find positions of two matching columns.
City1 | City2 |
Lahore | Lahore |
Karachi | Islamabad |
Multan | Karachi |
Multan | Multan |
Lahore | Islamabad |
To find the positions of two matching columns, we first initialize a pandas
dataframe with two columns of city names. Then we use where()
of numpy
to compare the values of two columns.
This returns an array that represents the indices where the two columns have the same value.
#importing pandas and numpy librariesimport pandas as pdimport numpy as np#initializing pandas dataframedf = pd.DataFrame({'City1': np.random.choice(['Lahore', 'Multan', 'Karachi'], 10),'City2': np.random.choice(['Karachi', 'Islamabad', 'Multan'], 10)})#displaying initialized dataframeprint(df)#displaying columns where values match in a dataframeprint(np.where(df.City1 == df.City2))