Python – How to populate the first N/A cell when applying a rolling average to a column – python

How to populate the first N/A cell when applying a rolling average to a column – python… here is a solution to the problem.

How to populate the first N/A cell when applying a rolling average to a column – python

I need to apply a rolling mean

to a column, as shown in Figure 1 s3, after I apply the rolling mean and set window = 5, I get the correct answer, but the first 4 rows are left blank, as shown in Figure 2 sa3.

I want to fill the first 1 empty cells in PIC1 SA3 with the average of all data in PIC2 S3 up to the current 4 as shown in PIC2 A3.

How do I use a simple function other than the rolling average method.
pic1

pic2

pic3

Solution

I think >rolling The parameter min_periods=1 :

min_periods : int, default None

Minimum number of observations in window required to have a value (otherwise result is NA). For a window that is specified by an offset, this will default to 1.

df = df.rolling(5, min_periods=1).mean()

Example:

np.random.seed(1256)

df = pd. DataFrame(np.random.randint(10, size=(10, 5)), columns=list('abcde'))
print (df)
   a  b  c  d  e
0  1  5  8  8  9
1  3  6  3  0  6
2  7  0  1  5  1
3  6  6  5  0  4
4  4  9  4  6  1
5  7  7  5  8  3
6  0  7  2  8  2
7  4  8  3  5  5
8  8  2  0  9  2
9  4  7  1  5  1

df = df.rolling(5, min_periods=1).mean()
print (df)
          a         b     c         d         e
0  1.000000  5.000000  8.00  8.000000  9.000000
1  2.000000  5.500000  5.50  4.000000  7.500000
2  3.666667  3.666667  4.00  4.333333  5.333333
3  4.250000  4.250000  4.25  3.250000  5.000000
4  4.200000  5.200000  4.20  3.800000  4.200000
5  5.400000  5.600000  3.60  3.800000  3.000000
6  4.800000  5.800000  3.40  5.400000  2.200000
7  4.200000  7.400000  3.80  5.400000  3.000000
8  4.600000  6.600000  2.80  7.200000  2.600000
9  4.600000  6.200000  2.20  7.000000  2.600000

Related Problems and Solutions