简体   繁体   中英

How to categorize text in a pandas dataframe based on the number of positive and negative keywords

I have a pandas data frame which looks like this:

          Tweets                      negative_keywords positive_keywords
0   Şanlıurfa'da DAEŞ ile                       []            []
1   Hacettepe Üni. Araştırması                  []            []
2   Kadına şiddetin suç olduğu                [suç]           []
3   Suriyeli'lerin fal bakabilme                []            []
4   Hastaneye git Suriyeli. PTT ye              []      [kardeşi]

I want to add a new column which is called neutral_keywords. If negative_keywords and positive_keywords are [] at the same time, neutral_keywords should take the value of [neutral]. Otherwise, it should take the value of []. Then, I should add a new column based on the values of positive, negative and neutral. So, my new pandas data frame should look like this:

          Tweets                      negative_keywords positive_keywords keyword_category  keyword_category
0   Şanlıurfa'da DAEŞ ile                       []            []             [neutral]       neutral
1   Hacettepe Üni. Araştırması                  []            []             [neutral]       neutral 
2   Kadına şiddetin suç olduğu                [suç]           []                []           negative
3   Suriyeli'lerin fal bakabilme                []            []             [neutral]       neutral
4   Hastaneye git Suriyeli. PTT ye              []         [kardeşi]           []            positive

How can I do that?

Assuming the data in df are lists of strings, here's how I would do it.

n_negative = df['negative_keywords'].apply(len)
n_positive = df['positive_keywords'].apply(len)
df['keyword_category'] = 'neutral'
df.loc[n_negative > 0, 'keyword_category'] = 'negative'
df.loc[n_positive > 0, 'keyword_category'] = 'positive'  # May over-write negatives

Output:

>>> df
                           Tweets negative_keywords positive_keywords keyword_category
0           Şanlıurfa'da DAEŞ ile                []                []          neutral
1      Hacettepe Üni. Araştırması                []                []          neutral
2      Kadına şiddetin suç olduğu             [suç]                []         negative
3    Suriyeli'lerin fal bakabilme                []                []          neutral
4  Hastaneye git Suriyeli. PTT ye                []         [kardeşi]         positive

One alternative you might want to consider is:

n_negative = df['negative_keywords'].apply(len)
n_positive = df['positive_keywords'].apply(len)
df['keyword_category'] = 'neutral'
df.loc[n_negative > n_positive, 'keyword_category'] = 'negative'
df.loc[n_positive > n_negative, 'keyword_category'] = 'positive'
# define a function which returns True if all are empty lists all_empty = lambda x: all(not lst for lst in x) # apply function to the two columns to create a mask mask = df[['negative_keywords', 'positive_keywords']].apply(all_empty, axis=1) # initialize the neutral_keywords column df['neutral_keywords'] = [[]] * len(mask) # update the neutral_keywords column where the mask is True df.loc[mask, 'neutral_keywords'] = [['neutral']] * mask.sum() df

This can be an alternative solution.

df["keyword_category"] = ['negative' if n else 'positive' if p else 'neutral' 
                            for n, p in zip(df['negative_keywords'], df['positive_keywords'])]

Result:

                           Tweets negative_keywords positive_keywords keyword_category
0            Şanlıurfada DAEŞ ile                []                []          neutral
1      Hacettepe Üni. Araştırması                []                []          neutral
2      Kadına şiddetin suç olduğu             [suç]                []         negative
3     Suriyelilerin fal bakabilme                []                []          neutral
4  Hastaneye git Suriyeli. PTT ye                []         [kardeşi]         positive

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM