'how can I fill NaN values with mean by using a for loop in python
I have to do this for column h?
a b c d e f g h j
0 mid low excellent mid stable stable NaN 15.0 A
1 mid high excellent NaN unstable stable stable 10.0 S
2 high low excellent high stable stable mod-stable 10.0 A
3 mid NaN good high stable unstable mod-stable NaN A
4 mid mid excellent high stable stable stable 10.0 A
5 high low good NaN stable stable unstable 15.0 S
6 mid low excellent high stable stable mod-stable 5.0 S
7 high mid excellent mid unstable unstable stable 10.0 S
8 mid high good mid stable stable stable 10.0 S
9 mid low excellent mid unstable stable mod-stable 10.0 S
Solution 1:[1]
With mean of what?
If you use pandas or numpy you can check if value is pd.NA or np.nan. If you treat it like a text you can try splitting it with str.split:
for row in rows:
fields = row.split(delim)
if fields[7] == 'NaN':
fields[7] = str(mean)
row = fields.join(delim)
delim - delimiter you use in file
Or something along these lines. Hope that answers your question but I'm not sure what you're trying to do.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | wjandrea |
