用3列重塑 Dataframe

0pizxfdo  于 2021-08-25  发布在  Java
关注(0)|答案(3)|浏览(462)

我有以下代码:

sentiments = ['He is good', 'He is bad', 'She love her', 'She is fine with it', 'I like going outside', 'Its okay']
positive = [1,0,1,0,1,0]
negative = [0,1,0,0,0,0]
neutral = [0,0,0,1,0,1]
neutral
df = pd.DataFrame({'Sentiments':sentiments, 'Positives':positive, 'Negatives': negative, 'Neutrals':neutral})
df.head()

这就产生了:

我只想有两列,一列是情绪,另一列是类别,这应该是特定情绪,即结果应该是:
情感分类是积极的我的消极的

j5fpnvbx

j5fpnvbx1#

尝试 .melt() :

x = df.melt("Sentiments", var_name="Category")
x = x[x.value != 0].drop(columns="value")
x["Category"] = x["Category"].str.replace(r"s$", "", regex=True)
print(x)

印刷品:

Sentiments  Category
0             He is good  Positive
2           She love her  Positive
4   I like going outside  Positive
7              He is bad  Negative
15   She is fine with it   Neutral
17              Its okay   Neutral
fcg9iug3

fcg9iug32#

假设只有一列的值为1(即dummies),请尝试:

>>> df.set_index("Sentiments").idxmax(axis=1).rename("Category").reset_index()
             Sentiments   Category
0            He is good  Positives
1             He is bad  Negatives
2          She love her  Positives
3   She is fine with it   Neutrals
4  I like going outside  Positives
5              Its okay   Neutrals
tktrz96b

tktrz96b3#

另一种方式:

df = df.set_index('Sentiments').dot(df.columns[1:]).reset_index(name = 'Category')

相关问题