我将解释完整的上下文,以防万一,我找到了一些解决方案,但仅使用显式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
,但我希望它与星期几列匹配,我的意思是:如果在一个记录中,我有3个(意思是星期三(在day_of_week列。我想,在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来做到这一点,如下所示:
for i in range(7):
data_set.loc[data_set['hours'] == i, 'probs' ] = temp_prob_by_field.loc[i]
我对熊猫真的很陌生,在我看来这不是解决这个问题的好方法,也许我错了。
作为一个@not_speshai作为一个数据样本玩:
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
"""
我想您要找的是pd.merge
。尝试:
data.merge(temp_prob_by_field, left_on="day_of_week", right_index=True)