0

I have dataframe like this:

    text            emotion
0   working add oil [1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0]
1   you're welcome  [0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0]
7   off to face my exam now [0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, ...
12  no, i'm so not la! i want to sleeeeeeeeeeep.    [0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, ...
151 i try to register on ebay. when i enter my hom...   [1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, ...
18  Swam 6050 yards on just a yogurt for breakfast...   [0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, ...
19  Alright!    [0, 0, 1, 1, 0, 0, 0, 0]
120 Visiting gma. It's getting cold [0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, ...
22  You are very missed [0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, ...
345 ...LOL! You mean Rhode Island...close enough    [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, ...

How can I leave only the first numbers in emotion column, to get data like this?:

    text            emotion
0   working add oil 1
1   you're welcome  0
7   off to face my exam now 0
12  no, i'm so not la! i want to sleeeeeeeeeeep.    0
151 i try to register on ebay. when i enter my hom...   1 
18  Swam 6050 yards on just a yogurt for breakfast...   0 
19  Alright!    **0**
120 Visiting gma. It's getting cold 0
22  You are very missed **0**
345 ...LOL! You mean Rhode Island...close enough    0

enter image description here

1 Answers1

0

If "emotion" column is a list and not string:

df["emotion"] = df["emotion"].apply(lambda x: x[0])
print(df)

Prints:

                                                text  emotion
0                                    working add oil        1
1                                     you're welcome        0
2                            off to face my exam now        0
3       no, i'm so not la! i want to sleeeeeeeeeeep.        0
4  i try to register on ebay. when i enter my hom...        1
5  Swam 6050 yards on just a yogurt for breakfast...        0
6                                           Alright!        0
7                    Visiting gma. It's getting cold        0
8                                You are very missed        0
9       ...LOL! You mean Rhode Island...close enough        0

If it's string, you can convert it to list using ast.literal_eval:

from ast import literal_eval

df["emotion"] = df["emotion"].apply(literal_eval)
# and then:
df["emotion"] = df["emotion"].apply(lambda x: x[0])
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91