0

Posting minimal reproducible example

I have a dataframe say

df  
           combined
0    [0, 0, 1, 0, 0, 1, 0, 1]
1    [1, 0, 0, 0, 0, 1, 1, 0]
2             [1, 0, 0, 0, 1]

I need to create a new column where values will be only from the alternate index.

       new_column
0    [0, 1, 0, 0]
1    [1, 0, 0, 1]
2    [1, 0, 1]

This is what I tried, but this does not work

df['combined'].str[::2]

P.S : There are numerous ways in which this can be achieved I am looking for a more pandaic approach

1 Answer 1

1

You may need to check your cell type is list if not do ast

import ast
df['combined'] = df['combined'].apply(ast.literal_eval)
df['combined'].str[::2]
Sign up to request clarification or add additional context in comments.

Comments

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.