0

I have this dataframe:

text           sentiment
asdasda        positive
fsdfsdfs       negative
sdfsdfs        neutral
dfsdsd         mixed

and I want this outupu:

text           positive     negative     neutral      mixed
asdasda           1            0           0           0
fsdfsdfs          0            1           0           0
sdfsdfs           0            0           1           0
dfsdsd            0            0           0           1

How can I do it?

1 Answer 1

1

You can use pandas.get_dummies but before that you need to set column "text" as index and after getting result you need to rename all columns sentiment_positive to positive , sentiment_negative to negative, ...

import pandas as pd
# df <- your_df
res = pd.get_dummies(df.set_index('text')
                     # rename column sentiment_positive to positive , 
                     # rename column sentiment_negative to negative , ...
                    ).rename(columns = lambda x: x.split('_')[1])
print(res)

          mixed  negative  neutral  positive
text                                        
asdasda       0         0        0         1
fsdfsdfs      0         1        0         0
sdfsdfs       0         0        1         0
dfsdsd        1         0        0         0
Sign up to request clarification or add additional context in comments.

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.