简体   繁体   中英

How can I change a column value based on length of element in a DataFrame

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 ? Eg 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.

Use numpy.where by condition by len and isin :

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM