Reputation: 21
I have a dataframe that looks like this:
df = pd.read.csv("variants.csv")
print(df)
Sample Position Reference Alt
Sample1 123 C T
Sample2 123 C T
Sample3 1234 C G
and a list of 'Positions' of interest:
pos = [123, 1334, 1443, 133]
I want to map the column of 'Position' to my list and create a new column in the dataframe where if 'Position' exists == 1 else I assign a value of 0. Example:
Sample Position Reference Alt Value
Sample1 123 C T 1
Sample2 123 C T 1
Sample3 1234 C G 0
How can I do this in Pandas or another way? Apologies as I'm a beginner to python.
Upvotes: 2
Views: 392
Reputation: 863541
Use Series.isin
for mask and then convert to 1,0
from True, False
by Series.view
:
df['Value'] = df.Position.isin(pos).view('i1')
Or by cast to integers by Series.astype
:
df['Value'] = df.Position.isin(pos).astype('int')
Or with numpy.where
:
df['Value'] = np.where(df.Position.isin(pos), 1, 0)
Upvotes: 3