Skip to content Skip to sidebar Skip to footer

How Can I Fill Gaps By Mean In Period Datetime Column In Pandas Dataframe?

I have a dataframe like below: df = pd.DataFrame({'price': ['480,000,000','477,000,000', '608,700,000', '580,000,000', '350,000,000'], 'sale_date': ['1396/10/30','1396/10/30', '139

Solution 1:

You can first reindex without replace missing values to 0 by fill_value parameter, then forward and fill missiing values with sum by add and last divide by 2:

df['sale_date']=df['sale_date'].str.replace('/','').astype(int)
df['price'] = df['price'].str.replace(',','').astype(int)

def conv(x):
    return pd.Period(year=x // 10000,
                     month=x // 100 % 100,
                     day=x % 100, freq='D')
 
df['sale_date'] = df['sale_date'].apply(conv)

s = df.groupby('sale_date')['price'].sum()

rng = pd.period_range(s.index.min(), s.index.max(), name='sale_date')
s = s.reindex(rng)
print (s)
sale_date
1396-10-30    957000000.0
1396-10-31            NaN
1396-11-01    608700000.0
1396-11-02            NaN
1396-11-03    580000000.0
1396-11-04            NaN
1396-11-05            NaN
1396-11-06            NaN
1396-11-07    350000000.0
Freq: D, Name: price, dtype: float64

s = s.ffill().add(s.bfill()).div(2).reset_index()
print (s)
    sale_date        price
0  1396-10-30  957000000.0
1  1396-10-31  782850000.0
2  1396-11-01  608700000.0
3  1396-11-02  594350000.0
4  1396-11-03  580000000.0
5  1396-11-04  465000000.0
6  1396-11-05  465000000.0
7  1396-11-06  465000000.0
8  1396-11-07  350000000.0

print ((957000000 + 608700000)/ 2)
782850000.0

Post a Comment for "How Can I Fill Gaps By Mean In Period Datetime Column In Pandas Dataframe?"