【问题标题】:How to fill a column given a condition that check a list on a index and assign given that index如何在给定检查索引列表的条件下填充列并在给定索引的情况下分配
【发布时间】:2021-08-24 15:04:02
【问题描述】:

我将解释完整的上下文以防万一,我找到了一些解决方案,但只能使用明确的for i in range 或设置一个简单的条件,而不是像我需要的那样。

我有一个包含以下列的数据框:postauthorDateTimeday_of_weekhours

现在我想计算以下概率: that any author post a post on a specific day of the weeknumber_post_that_week_day/total_post

这很简单,可以按以下方式完成(可能不是最好的方法,但可以接受):

count_by_field = data_set.groupby('day_of_week').count()['post']
total_by_field = data_set.groupby('day_of_week').count()['post'].sum()
temp_prob_by_field = count_by_field / total_by_field

# In case I need that the size of temp_prob_by_field should be 7
# but my sample, in some cases, only has Monday, Saturday
# With the next lines I will always have 7 records 
for index in range(size):
        if not index in temp_prob_by_field.index:
            temp_prob_by_field.loc[index] = 0

问题

我想将我的概率值分配给新列 (prob) 上的原始 data_set,但我希望它与星期几列匹配,我的意思是: 如果在记录中,我在 day_of_week 列上有 3 个(这意味着星期三)。我想要,在probs 列上的那条记录中关联的概率。

我一直在尝试的(没有成功):

data_set[data_set.loc[ data_set['hours'] in  temp_prob_by_field.index, temp_prob_by_field ]] 
= temp_prob_by_field.loc[data_set.loc[ data_set['hours'] in  temp_prob_by_field.index] # ????‍♂️

我可以通过下面的 for in 来做到这一点:

for i in range(7):
  data_set.loc[data_set['hours'] == i, 'probs' ] = temp_prob_by_field.loc[i]

我真的是 pandas 的新手,在我看来这不是解决这个问题的好方法,也许我错了。

作为 @not_speshai 作为 data_sample 玩:

import pandas as pd
import numpy as np
np.random.seed(1213)
c = ['post', 'author', 'datetime', 'day_of_week', 'hours']
data = pd.DataFrame(np.random.choice([1,0,3,5], size=(10,5)), columns=c)
data['post']='A post about something"


"""                  post  author  datetime  day_of_week  hours
0  A post about something       5         5            0      3
1  A post about something       1         1            1      5
2  A post about something       3         1            3      5
3  A post about something       5         3            5      1
4  A post about something       0         5            3      0
5  A post about something       3         3            0      1
6  A post about something       0         5            5      0
7  A post about something       3         3            5      3
8  A post about something       5         1            1      0
9  A post about something       1         0            0      3
"""

【问题讨论】:

  • 请您发布data_set 的样本和预期的输出?
  • @not_speshal 我认为在这种情况下不需要它,但无论如何,我只是编辑帖子并添加一个示例。

标签: python pandas conditional-statements multiple-columns assign


【解决方案1】:

我认为您正在寻找的是pd.merge。试试:

data.merge(temp_prob_by_field, left_on="day_of_week", right_index=True)

【讨论】:

  • @Me the scripter - 另外,您计算概率的逻辑与您在问题中描述的不匹配。如果您正在寻找您提到的number_post_that_week_day/total_post,您想做:temp_prob_by_field = data.groupby("day_of_week")["post"].sum()/data["post"].sum()
  • 我误会了你。你需要数一数,帖子上的数字只是为了这个例子,为了测试,帖子上的数字只是消息,而不是帖子的数量。我将编辑描述。我需要计算data_set和sum中有多少帖子
  • 如果您确定您的计算是正确的,请这样做:temp_prob_by_field = data_set.groupby('day_of_week')['post'].count()/data_set.groupby('day_of_week')['post'].count().sum()。此外,这正是我要求提供样本数据的原因——它可以消除很多误解 :)
猜你喜欢
  • 2016-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-12
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多