import pandas as pd
df = pd.read_excel('mydata2.xlsx')
values_in_A = set(df['A'])
values_in_B = set(df['B'])
common_values = list(values_in_A.intersection(values_in_B))
matched_df = df[(df['A'].isin(common_values)) | (df['B'].isin(common_values))]
matched_df.to_excel('matched_values.xlsx', index=False)
Import Pandas: import pandas as pd
- Imports the Pandas library and aliases it as pd
for ease of use.
Read Excel Data: df = pd.read_excel('mydata2.xlsx')
- Reads an Excel file named mydata2.xlsx
and stores its contents in a Pandas DataFrame called df
.
Extract Unique Values:
values_in_A = set(df['A'])
and values_in_B = set(df['B'])
- Extracts unique values from columns 'A' and 'B' and stores them in sets.Find Common Values: common_values = list(values_in_A.intersection(values_in_B))
- Determines the common values existing in both 'A' and 'B' columns by finding the intersection of the two sets.
Creates a new DataFrame, matched_df
, containing rows where either column 'A' or 'B' has values that match the common values found in the previous step
Write Filtered Data to Excel: matched_df.to_excel('matched_values.xlsx', index=False)
- Writes the filtered DataFrame to a new Excel file named matched_values.xlsx
without including the DataFrame index as a column in the output file.
No module named PIL Install PIL module: If you haven't installed PIL yet, install it using pip, the..
Lambda is a small and anonymous function in Python, where you can use many arguments but only ..
Connect with mysql database using python Install the mysql-connector-python-rf - $..
Insert data into MySQL table using Python. Install mysqlclient Open Terminal..
Get the latest news and updates by signing up to our daily newsletter.We won't sell your email or spam you !