[英]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.