2

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.

1 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

1 Comment

never seen view before, nice!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.