【发布时间】:2021-08-24 15:04:02
【问题描述】:
我将解释完整的上下文以防万一,我找到了一些解决方案,但只能使用明确的for i in range 或设置一个简单的条件,而不是像我需要的那样。
我有一个包含以下列的数据框:post、author、DateTime、day_of_week、hours
现在我想计算以下概率:
that any author post a post on a specific day of the week 即number_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