[英]Create a new Dataframe that counts positive and negative tweets for each user
我有以下 DataFrame:
它包含 user_ids、推文、位置以及推文的分類為負面和正面。
我想創建一個按用戶 ID 分組的新 dataframe,因為每個用戶在 dataframe 中有不止一條推文。 dataframe 應包含以下列:
所需樣品 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.