Python Forum
How to fill empty values with mean
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to fill empty values with mean
#1
I have below DataFrame,

1.0  2.1
1.5  
     0.9
4.8   
1.7  9.0
2.0  1.0

8.7  2.9
1.8  1.1
     0.6 
2.0  3.2
I want to replace empty cells with mean uptnes the empty row, and in the next empty row, again take the mean of of the row after last empty row. For example

Rows are number from 0,1,...n (follow the python notation)
Case1: For 1st column, 2rd row id empty and the average of row1~row2 is 1.25
case2: column1, row6 is empty, and fill with the average calculated from row3~row5.
Case3:Row9 in column1 is empty, and filled with the average calculated using row7&row8
Same rule follow in the column2. I am new to Python, I only knew pd.drop function to drop the row meet the criterion.Please kindly some one help how to do to replace the empty row with mean.

Desired output:

1.00  2.10
1.50  2.10
1.25  0.90
4.80  0.90
1.70  9.00
2.00  1.00
2.80  5.00
8.70  2.90
1.80  1.10
5.25  0.60
2.00  3.20
Reply
#2
Do want to replace the blanks with 0?

Not sure where the 1.25 line 3 or 2.80 5.00 is coming from.
Gary
Reply
#3
I don't want to replace empty with '0's or remove them. But, I want to replace the empty cell with mean of the above rows.
1.25 in row3 is becasue: The row3 in column1 in original data is an empty cell, and I replace with the mean of row1& row2(of column1)i.e, meanof 1 & 1.5 is 1.25.

About 2.80: is: Because in original data, the row7 is empty, and it is replace with mean of row4,row5,row6
I did not take the average of row1~row6, because row3 was empty in original data, I omit row1~row3.
Row10 was empty in original data, and it is replace with the average of row8 &row9 (brcasue row7 was empty in original data, adn removed all rows up to row7.
Reply
#4
Sorry but still not sure what your trying to do.

Can you please put your code.

Thanks
Gary
Reply
#5
I want to replace empty cells with mean.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to generate rows based on values in a column to fill missing values codesmatter 1 2,093 Oct-31-2020, 12:05 AM
Last Post: Larz60+
  Unable to assign selected values from existing list to an empty list haiderhussain 5 4,473 Jan-19-2017, 05:57 AM
Last Post: haiderhussain

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020