When complementing DataFrame/Series deficiencies with constants, you want to specify only interpolation.

Asked 2 years ago, Updated 2 years ago, 273 views

I'd like to supplement the missing DataFrame/Series values with constants. Is there a way to specify the interpolation [limit_area='inside']?
I don't want to make up for the missing values on both ends.

import pandas as pd

series=pd.Series(data=[None, 20, None, 40, None], index=['aa', 'bb', 'cc', 'dd', 'ee', name='value')
print('◇original data')
display(series.to_frame())

# I want to specify the inside option as in series=series.fillna(0,limit_area='inside')#←series.interpolate().
print(' ◇↓ expected defect value interpolation result (I want to insert it internally with a constant).I don't want to extrapolate.)')
series=pd.Series(data=[None, 20, 0, 40, None], index=['aa', 'bb', 'cc', 'dd', 'ee', name='value')
display(series.to_frame())

Enter a description of the image here

Thank you for your reply.
The first_valid_index/last_valid_index method was successful.

################################################################################
import pandas as pd
series = pd.Series(
    # data=[ None, None, None, None, None, None ],
    data=[ None, None, 20, None, 40, None ],
    index=[
        pd.Timestamp('2022/01/10'),
        pd.Timestamp('2022/01/11'),
        pd.Timestamp('2022/01/12'),
        pd.Timestamp('2022/01/13'),
        pd.Timestamp('2022/01/14'),
        pd.Timestamp('2022/01/15')
    ], name='value'
)
pr int (' Enhancing the original data ' )
display(series.to_frame())

index_low = series.first_valid_index()
index_high=series.last_valid_index()
if index_low is not None:
    series.loc [index_low:index_high] = series.loc [index_low:index_high].fillna(0)
display(series.to_frame())

python python3 pandas

2022-09-30 22:02

1 Answers

Thank you.
What should I do if I don't know how many missing values at both ends will continue?
Sorry for the unclear question.

import pandas as pd
series = pd.Series(
    data=[ None, None, 20, None, 40, None ],
    index=[
        pd.Timestamp('2022/01/10'),
        pd.Timestamp('2022/01/11'),
        pd.Timestamp('2022/01/12'),
        pd.Timestamp('2022/01/13'),
        pd.Timestamp('2022/01/14'),
        pd.Timestamp('2022/01/15')
    ], name='value'
)
pr int (' Enhancing the original data ' )
display(series.to_frame())

Enter a description of the image here


2022-09-30 22:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.