0

I am new to working with Python and have the following problem with my calculations. I have a table which contains NaN values. The NaN values always occur at night, because no solar radiation can be measured there. I want to replace all NaN values from a night with the value 4 hours before sunset. I already tried to use the Ffill command, but since I don't need the last value before the NaN values, it doesn't work unfortunately.

For example:

a=[0.88, 0.84, 0.26, 0.50, 1.17, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 0.73, 0.81]

The successive NaN values should all have the value 0.84. The list should therefore look like this:

a=[0.88, 0.84, 0.26, 0.50, 1.17, 0.84, 0.84, 0.84, 0.84, 0.84, 0.84, 0.84, 0.84, 0.73, 0.81]

Thanks in advance.

1
  • If you use a dataframe, please update your post with a sample. Commented Aug 11, 2021 at 13:43

3 Answers 3

2

One option is to create a shifted and ffilled version of the original series and then just using that to fill in the nulls of the original data:

In [231]: s.fillna(s.shift(3).mask(s.isnull()).ffill())
Out[231]:
0     0.88
1     0.84
2     0.26
3     0.50
4     1.17
5     0.84
6     0.84
7     0.84
8     0.84
9     0.84
10    0.84
11    0.84
12    0.84
13    0.73
14    0.81
dtype: float64
Sign up to request clarification or add additional context in comments.

3 Comments

Is there a possibility to extend the code so that the output of the former NaN values and the value that is decisive for the new NaN values (here 0.84) are displayed in bold?
That mostly depends on your environment that you're running this in. If you're in a notebook, pandas has some styling options: pandas.pydata.org/pandas-docs/stable/user_guide/…
I am working with Jupiter notebook. I am able to make a column bold but not the new calculated values.. Could you tell me how it works?
0
import pandas as pd

a = [0.88, 0.84, 0.26, 0.50, 1.17, None, None, None, None, None, None, None, None, 0.73, 0.81]
df = pd.DataFrame(a)
df[3:] = df[3:].fillna(value=df.iloc[1, 0])
print(df)
       0
0   0.88
1   0.84
2   0.26
3   0.50
4   1.17
5   0.84
6   0.84
7   0.84
8   0.84
9   0.84
10  0.84
11  0.84
12  0.84
13  0.73
14  0.81

1 Comment

This requires the array to have the nulls start appearing exactly at index 5.
0
a=[0.88, 0.84, 0.26, 0.50, 1.17, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, 0.73, 0.81]
a = np.array(a)
a[np.isnan(a)] = a[1]
a

results:

array([0.88, 0.84, 0.26, 0.5 , 1.17, 0.84, 0.84, 0.84, 0.84, 0.84, 0.84,
       0.84, 0.84, 0.73, 0.81])

1 Comment

This requires the array to have the nulls start appearing exactly at index 5.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.