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

时间:2020-09-25 12:17:17

标签: python pandas

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

col_1            
[A, A, A, B, C]
[D, B, C]
[C]
[A, A, A]
NaN

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

col_1              new_col           
[A, A, A, B, C]    1
[D, B, C]          0
[C]                0
[A, A, A]          1
NaN                0

我尝试了这个,但是没用:

df['new_col'] = df.loc[df.col_1[0:3] == [A, A, A]]

3 个答案:

答案 0 :(得分:2)

由于存在一些非列表值,如果没有列表,可以对if-else使用0 lambda函数:

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

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

答案 1 :(得分:1)

通过应用lambda解决方案:

df = pd.DataFrame({
    'col_1': [          
      ['A', 'A', 'A', 'B', 'C'],
      ['D', 'B', 'C'],
      ['C'],
      ['A', 'A', 'A']
    ]
})

df['new_col'] = df.col_1.apply(lambda x: x[0:3] == ['A', 'A', 'A'] if isinstance(x, list) else False).view('i1')

df.head()

输出:

Output

答案 2 :(得分:1)

这是使用map的另一种可能的解决方案:

import pandas as pd

#borrowing dataframe from @Alexendra
df = pd.DataFrame({
    'col_1': [
      ['A', 'A', 'A', 'B', 'C'],
      ['D', 'B', 'C'],
      ['C'],
      ['A', 'A', 'A']
    ]
})

df['new_col'] = df['col_1'].map(lambda x : 1  if x[0:3] == ['A','A','A']   else 0)

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