[英]Fill NANs in a dataFrame with values with same atributes such as weekday and hour ( Pandas)
我想知道如何在一周的同一小時和同一天以其他方式替換DataFrame的NANS。
例如,從周三的11:00 pm到周四的10:00 am,我有幾個NAN。 與月份無關,僅與小時和工作日無關。 我所做的是創建另一個dataFrame調用dfgrouped:
dfgrouped = df.groupby(['Weekday','Hour'])。mean()
現在我有了這個dataFrame,如何將其與df.isnull()結合使用?
有沒有更簡單的方法可以做到這一點?
提前致謝。
這是我對您問題的理解。
sample = {'Activa': {0: np.nan, 1: 328.76750000000004}, 'Aparente': {0: np.nan, 1: 332.28750000000002}, 'Building': {0: 'Quimica', 1: 'Quimica'}, 'Hour': {0: 13, 1: 14}, 'Month': {0: 'Jun', 1: 'Jun'}, 'Month_num': {0: 6, 1: 6}, 'Reactiva': {0: -70.599999999999994, 1: -46.682500000000005}, 'Timestamp': {0: pd.to_datetime('2012-06-01 13:00:00'), 1: pd.to_datetime('2012-06-01 14:00:00')}, 'Week': {0: 22, 1: 22}, 'Weekday': {0: 'Fri', 1: 'Fri'}, 'Weekday_num': {0: 4, 1: 4}, 'Year': {0: 2012, 1: 2012}}
# There is a nan value in the activa column
df = DataFrame(sample)
# Performing the groupby as you do
df_grouped = df.groupby(['Week','Weekday']).mean()
# Setting the same index on the original DataFrame
df = df.set_index(['Week','Weekday'])
# Filling nan values with the mean
df = df.fillna(df_grouped)
df
# The result
Activa Aparente Building Hour Month Month_num Reactiva \
Week Weekday
22 Fri 328.7675 332.2875 Quimica 13 Jun 6 -70.6000
Fri 328.7675 332.2875 Quimica 14 Jun 6 -46.6825
Timestamp Weekday_num
Week Weekday
22 Fri 2012-06-01 13:00:00 4
Fri 2012-06-01 14:00:00 4
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.