【问题标题】:How do I iterate over the rows of a specific Pandas DataFrame column, given a condition from another column?给定另一列的条件,如何迭代特定 Pandas DataFrame 列的行?
【发布时间】:2020-05-27 04:38:52
【问题描述】:

所以我基本上想做的是以下内容,基于具有“日期”和“极性”列的数据框,“日期”(天)中有七个不同的值,“极性”中的值介于 -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

【问题讨论】:

  • 您能否提供一个示例数据集以及预期的输出?
  • 添加了预期的输出。数据集是一个 Excel 表格,列格式为“日期”(YYYY-MM-DD),格式为“极性”(每行的值介于 -1 和 1 之间)。

标签: python pandas loops dataframe tweepy


【解决方案1】:

如果我理解正确,您需要计算每一天的极性值。 可以是这样的:

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 的极性计数。

【讨论】:

    【解决方案2】:

    考虑一个带边距的pivot_table。下面使用随机的种子数据进行演示:

    数据

    import numpy as np
    import pandas as pd
    
    np.random.seed(2112020)
    random_df = pd.DataFrame({'date': np.random.choice(pd.date_range('2020-02-01', '2020-02-11'), 500),
                              'polarity': np.random.randint(-1, 2, 500)})
    
    print(random_df.head(10))
    #         date  polarity
    # 0 2020-02-08        -1
    # 1 2020-02-08         1
    # 2 2020-02-06         0
    # 3 2020-02-10        -1
    # 4 2020-02-04        -1
    # 5 2020-02-02         1
    # 6 2020-02-05        -1
    # 7 2020-02-04         0
    # 8 2020-02-10         1
    # 9 2020-02-09         0
    

    聚合

    pvt_df = (random_df.assign(day_date = lambda x: x['date'].dt.normalize(),
                               polarity_indicator = lambda x: np.select([x['polarity'] > 0, x['polarity'] < 0, x['polarity'] == 0],
                                                                        ['positive', 'negative', 'neutral']))
                       .pivot_table(index = 'day_date',
                                    columns = 'polarity_indicator',
                                    values = 'polarity',
                                    aggfunc = 'count',
                                    margins = True)
             )
    
    print(pvt_df)
    
    #  polarity_indicator   negative  neutral  positive  All
    #  day_date
    #  2020-02-01 00:00:00        17       14        16   47
    #  2020-02-02 00:00:00        19       14        12   45
    #  2020-02-03 00:00:00        11       16        12   39
    #  2020-02-04 00:00:00        17       18        13   48
    #  2020-02-05 00:00:00        11       15        22   48
    #  2020-02-06 00:00:00        12       12        16   40
    #  2020-02-07 00:00:00        16       15        21   52
    #  2020-02-08 00:00:00        15       10        13   38
    #  2020-02-09 00:00:00        17       15        19   51
    #  2020-02-10 00:00:00        13       16        19   48
    #  2020-02-11 00:00:00        13       12        19   44
    #  All                       161      157       182  500
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-03
      • 1970-01-01
      • 2014-12-29
      • 2021-07-17
      • 1970-01-01
      • 2022-06-15
      • 2018-03-02
      相关资源
      最近更新 更多