I am trying to:
- check if values in a range exist in a dataframe
- if not, add the value and interpolate.
Referring to this answer, I have checked that it works for a single dataframe. For example:
# Original dataframe
code ratio
...
5 5.0 1.649561
6 6.0 1.466403
7 11.0 1.696970
8 12.0 1.646259
# Code to add row + interpolate
for i in range(5, 13):
if i not in df.values:
df.loc[-1, 'code'] = i
df = df.sort_values('code').reset_index(drop=True)
df = df.interpolate()
# Result
code ratio
0 5.0 1.649561
1 6.0 1.466403
2 7.0 1.581686
3 8.0 1.639328
4 9.0 1.668149
5 10.0 1.682559
6 11.0 1.696970
7 12.0 1.646259
Checking that it worked on a single dataframe, I wanted it to be done on multiple dataframes I have. So I tried the following code, using a list of dataframes for iteration:
for df in [df1, df2, df3...]:
for i in range(5, 13):
if i not in df.values:
df.loc[-1, 'code'] = i
df = df.sort_values('code').reset_index(drop=True)
df = df.interpolate()
Then even for the dataframe that worked before, it returns:
code ratio
5 5.0 1.649561
6 6.0 1.466403
7 11.0 1.696970
8 12.0 1.646259
-1 7.0 NaN
Which is clearly not the result I want.
What causes this difference? Is using a list of multiple dataframes for iteration a wrong approach to this?