I have a Dataframe like this (the columns are not side by side):
Num A Num B Num C Marked
0 213 314 512
1 612 516 713
2 613 678 125
3 163 813 312
And a list like list = [612,813,512,713]
I now want to compare, if a value from the list is present in the dataframe, and mark it with 1, else with 0, so that the output is:
Num A Num B Num C Marked
0 213 314 512 1
1 612 516 713 1
2 613 678 125 0
3 163 813 312 1
I only found out how to do this with a single column:
import pandas as pd
import numpy as np
path = "path"
wb = pd.ExcelFile(path)
df = wb.parse("Sheet1")
list = [612,813,512,713]
df['Marked'] = df.Num_A.isin.(list).astype(int)
How can you make this to consider all columns?
Thanks in Advance!
CodePudding user response:
You can use np.isin
method to check if numbers in lst
exist in df
along columns and then convert it to integer value:
df['Marked'] = np.isin(df.values,lst).any(axis=1).astype(int)
CodePudding user response:
Generate a 2. df, use the isin there, add prefix to columns of second dataframe, then join them together:
lst = [612,813,512,713]
df.join(df.isin(lst).add_prefix('Marked_'))