用3列重塑 Dataframe

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

我有以下代码:

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

这就产生了:

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

j5fpnvbx

j5fpnvbx1#

尝试 .melt() :

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

印刷品:

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

fcg9iug32#

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

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

tktrz96b3#

另一种方式:

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

相关问题