给定另一列的条件,如何迭代特定Pandas DataFrame列的行?

问题描述 投票:0回答:1

因此,我基本上想要做的是基于一个具有'date'和'polarity'列的数据框,其中'date'(天)有七个不同的值,而'polarity'的值介于-1和1之间:

For each of the seven days:
i) count all values in the 'polarity' column that are positive
ii) count all values in the 'polarity' column that are negative
iii) count all values in the 'polarity' column for a given day (neg, neutral, pos)

编辑:对于每天i)-iii)中的每一个,输出应为整数,并存储在列表中

Edit2:我尝试使用以下代码实现它(仅适用于值> 0):

pos_tweets = df_tweets.apply(lambda x: True if x['polarity'] > 0 and x['date'] == '2020-02-07' else False, axis=1)
num_Pos = len(pos_tweets[pos_tweets == True].index)

但是,此值返回0,这是在Excel中签入时出错。

请多谢帮助!

干杯,IG

python pandas loops dataframe tweepy
1个回答
0
投票
positive = df_tweets[df_tweets['polarity'] > 0].groupby('date').count().reset_index() negative = df_tweets[df_tweets['polarity'] < 0].groupby('date').count().reset_index() neutral = df_tweets[df_tweets['polarity'] == 0].groupby('date').count().reset_index()

此代码的输出是具有两列的三个数据帧:一列具有日期的唯一值,一列具有极性更高,更少或等于0的计数。

© www.soinside.com 2019 - 2024. All rights reserved.