I don't understand what you are asking with this:
Quote:Should not all single digit sensors numbers be preceded. by a 0?
Are you asking if the code will pad single digit sensor numbers with a leading "0"? To that the answer is yes.
Are you asking why I am not padding all single digit sensor numbers (0 through 9)? This is an example. I forgot sensor_00.
Quote:Also, does it matter if all the number are in sequence or not?
This is not going to be allowed. You cannot have leading zeros in integer literals. Use regular numbers (no leading zero) and let the f"string pad the string, adding a zero when necessary.
for sensor in (01, 02, 03, 04, 05, 06, 07, 08, 09, 51):
label = f"sensor_{sensor:02}"
Or you could specify the sensors as strings. This would work. It is more typing and no better than using ints, but it will work.
for sensor in ("01", "02", "03", "04", "05", "06", "07", "08", "09", "51"):
label = f"sensor_{sensor}"
You should not be asking questions like this. This is the kind of thing where you should write a short program to test out your ideas, or in this case, your understanding of my ideas.
import numpy as np
import pandas as pd
# Make some data that includes NAN's
data = {
"sensor_01": [np.nan, 2.0, 3.0, 4.0, 5.0],
"sensor_02": [2.0, np.nan, 4.0, 5.0, 6.0],
"sensor_03": [3.0, 4.0, np.nan, 6.0, 7.0],
"sensor_04": [4.0, 5.0, 6.0, np.nan, 8.0],
}
df = pd.DataFrame(data)
print(df)
print(df.median()) # Print out the median values for each column
# Replace NAN's with column median values
for sensor in (3, 1): # Leaving out sensors 02 and 04. Will they get processed?
label = f"sensor_{sensor:02}"
df[label].fillna(df[label].median(), inplace=True)
print(df)
Output:
sensor_01 sensor_02 sensor_03 sensor_04
0 NaN 2.0 3.0 4.0
1 2.0 NaN 4.0 5.0
2 3.0 4.0 NaN 6.0
3 4.0 5.0 6.0 NaN
4 5.0 6.0 7.0 8.0
sensor_01 3.5
sensor_02 4.5
sensor_03 5.0
sensor_04 5.5
dtype: float64
sensor_01 sensor_02 sensor_03 sensor_04
0 3.5 2.0 3.0 4.0
1 2.0 NaN 4.0 5.0
2 3.0 4.0 5.0 6.0
3 4.0 5.0 6.0 NaN
4 5.0 6.0 7.0 8.0
As you can see, NaN in sensor_03 and sensor_01 have been replaced by median values from that column. NaN's in sensor_02 and sensor_04 were not replaced.
I do this with everything new that I try. First I write a short program to learn how it works. Then I write some test program to test boundary and unusual conditions (What if all the values are NaN?). Only after I think I really understand the new package or function do I add it to my main program.
Another benefit of writing a test program is you get to know that I screwed up in the example. Finding, and fixing, mistakes in other's code will you become better at finding your own mistakes. Why do you think I answer so many posts? With each one I learn something and I get a little better. Give me a decade and I'll be pretty good.
In case you were wondering, the median of (NaN, NaN, NaN, NaN, NaN) is NaN.