Reputation: 105
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?
Upvotes: 3
Views: 341
Reputation: 13393
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
Upvotes: 0
Reputation: 11613
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'
Upvotes: 2
Reputation: 6642
# 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
Upvotes: 1