簡體   English   中英

創建一個新的 Dataframe 計算每個用戶的正面和負面推文

[英]Create a new Dataframe that counts positive and negative tweets for each user

我有以下 DataFrame:

在此處輸入圖像描述

它包含 user_ids、推文、位置以及推文的分類為負面和正面。

我想創建一個按用戶 ID 分組的新 dataframe,因為每個用戶在 dataframe 中有不止一條推文。 dataframe 應包含以下列:

  1. 用戶身份
  2. 該 user_id 的負面推文計數
  3. 該 user_id 的正面推文計數
  4. 用戶的位置

所需樣品 output

user_id             positive_tweets   negative_tweets    Location
418                 1                    0                   CA
521                 1                    0                   CA
997                 0                    1                   LA
1135                1                    0                   LA

此代碼是 BlackFox 先生針對我之前未正確詢問的問題提出的。

df.groupby(['user_id','classification'])['user_id'].count()

但是,它與所需的 output 不匹配。

謝謝

我希望這就是你要找的。

df.groupby(['user_id', 'Location']).apply(lambda x: pd.Series(dict(
positive_tweets=(x.classification == 'positive').sum(),
negative_tweets=(x.classification == 'negative').sum(),
)))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM