根据熊猫列表列中的条件创建新列

kha*_*baa 5 python pandas

我有一个包含列表列的数据框:

col_1            
[A, A, A, B, C]
[D, B, C]
[C]
[A, A, A]
NaN
Run Code Online (Sandbox Code Playgroud)

我想创建新列,如果列表以 3* 开头A,则返回 1,否则返回 0:

col_1              new_col           
[A, A, A, B, C]    1
[D, B, C]          0
[C]                0
[A, A, A]          1
NaN                0
Run Code Online (Sandbox Code Playgroud)

我试过这个但没有用:

df['new_col'] = df.loc[df.col_1[0:3] == [A, A, A]]
Run Code Online (Sandbox Code Playgroud)

jez*_*ael 2

因为有一些非列表值可以使用if-elselambda 函数来表示0if not list:

print (df['col_1'].map(type))
0     <class 'list'>
1     <class 'list'>
2     <class 'list'>
3     <class 'list'>
4    <class 'float'>
Name: col_1, dtype: object
Run Code Online (Sandbox Code Playgroud)
f = lambda x: int((x[:3]) == ['A','A','A']) if isinstance(x, list) else 0
df['new_col'] = df['col_1'].map(f)
#alternative
#df['new_col'] = df['col_1'].apply(f)
print (df)
             col_1  new_col
0  [A, A, A, B, C]        1
1        [D, B, C]        0
2              [C]        0
3        [A, A, A]        1
4              NaN        0
Run Code Online (Sandbox Code Playgroud)