-1

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
wjandrea
  • 28,235
  • 9
  • 60
  • 81
ezzgicaan
  • 1
  • 2
  • 1
    Welcome to Stack Overflow! Please take the [tour]. What have you already tried? How are you loading this data? If it's a Pandas DataFrame, you don't need to use an explicit loop. See [ask] for more tips. You can [edit] to clarify. – wjandrea Apr 20 '22 at 15:33
  • Possible duplicate: [pandas DataFrame: replace nan values with average of columns](/q/18689823/4518341) – wjandrea Apr 20 '22 at 15:46

1 Answers1

0

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.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
piotre10
  • 43
  • 4
  • I want to replace NaN values with mean which I already calculated so there is no problem with mean. the problem is that: In the dataFrame there are some NaN values in h column and I want to fill them with mean which I calculated and It is required to do that with a for loop. I am really stuck. Thanks your help. And I just calculated the mean as 11 so ı should fill NaN values with 11 by using loop – ezzgicaan Apr 20 '22 at 16:23
  • So, then if you want to make a loop you can check `if field is pd.NA: `. Or as mentioned by wjandrea in post: [link](https://stackoverflow.com/questions/18689823/pandas-dataframe-replace-nan-values-with-average-of-columns) you can `find df.fillna()` function I think something like: `df['h'].fillna(mean)` will help – piotre10 Apr 20 '22 at 18:35
  • It works, thank you!! :) – ezzgicaan Apr 20 '22 at 18:52