Question: Guys,

For some reasons, I have to put a np.array into a single column of DataFrame. It looks as :

```
A           B        C
1       [1,2]        0
2         [4]        0
3   [1,2,5,6]        0
7     [2,5,6]        0
4         [8]        0

```

Is there any method setting the column C based on length of column B without iteracting them ? E.g. If length(col.B) == 2 or length(col.B) == 4, C = 1, else C = -1. Then I expected :

```
A           B        C
1       [1,2]        1
2         [4]       -1
3   [1,2,5,6]        1
7     [2,5,6]        1
4         [8]       -1

```

Thanks so much.

Answer: 
Use [`numpy.where`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html) by condition by [`len`](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.len.html) and [`isin`](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.isin.html):

```
df['C'] = np.where(df['B'].str.len().isin({2,4}), 1, -1)

print (df)
   A             B  C
0  1        [1, 2]  1
1  2           [4] -1
2  3  [1, 2, 5, 6]  1
3  7     [2, 5, 6] -1
4  4           [8] -1

```
Use `.apply`:

```
df['C']=df.apply(lambda row: 1 if len(row['B'].tolist()) in [2,4] else -1,axis=1)
print(df)

```

Output:

```
   A          B  C
0  1      [1,2]  1
1  2        [4] -1
2  3  [1,2,5,6]  1
3  7    [2,5,6] -1
4  4        [8] -1

```

(Do `ast.literal_eval` if the dataframe element are strings)