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
Advertisement
Answer
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]