5

I have a dataframe containing column of lists:

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

I want to create new column, if the list starts with 3* A return 1, if not return 0:

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

I tried this but didn't work:

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

3 Answers 3

2

Beause there are some non list values is possible use if-else lambda function for 0 if 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

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
Sign up to request clarification or add additional context in comments.

3 Comments

thanks, return an error :TypeError: 'float' object is not iterable you think from the NaN's ?
@khaledkoubaa - What is print (df['col_1'].map(type)) ?
@khaledkoubaa - If there are strings instead lists is possible use df['col_1'] = df['col_1'].str.strip('[]').str.split(',') before my solution for lists
2

Here is another potential solution using 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)

Output:

             col_1  new_col
0  [A, A, A, B, C]        1
1        [D, B, C]        0
2              [C]        0
3        [A, A, A]        1

Comments

1

Solution via apply 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:

Output

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.