in Education by
Starting Point I have a DataFrame df which has a three-level MultiIndex. The innermost level is a datetime. value data_1 data_2 data_3 data_4 id_1 id_2 effective_date ADH10685 CA1P0 2018-07-31 0.000048 17901701 3mra Actual 198.00 2018-08-31 0.000048 17901701 3mra Actual 198.00 CB0N0 2018-07-31 4.010784 17901701 3mra Actual 0.01 2018-08-31 2.044298 17901701 3mra Actual 0.01 2018-10-31 11.493831 17901701 3mra Actual 0.01 2018-11-30 13.929844 17901701 3mra Actual 0.01 2018-12-31 21.500490 17901701 3mra Actual 0.01 CB0P0 2018-07-31 22.389493 17901701 3mra Actual 0.03 2018-08-31 23.600726 17901701 3mra Actual 0.03 2018-09-30 45.105458 17901701 3mra Actual 0.03 2018-10-31 32.249056 17901701 3mra Actual 0.03 2018-11-30 60.790889 17901701 3mra Actual 0.03 2018-12-31 46.832914 17901701 3mra Actual 0.03 You can recreate this DataFrame with the following code: df = pd.DataFrame({'id_1': ['ADH10685','ADH10685','ADH10685','ADH10685','ADH10685','ADH10685','ADH10685','ADH10685','ADH10685','ADH10685','ADH10685','ADH10685','ADH10685'],\ 'id_2': ['CA1P0','CA1P0','CB0N0','CB0N0','CB0N0','CB0N0','CB0N0','CB0P0','CB0P0','CB0P0','CB0P0','CB0P0','CB0P0'],\ 'effective_date': ['2018-07-31', '2018-08-31', '2018-07-31', '2018-08-31', '2018-10-31', '2018-11-30', '2018-12-31', '2018-07-31', '2018-08-31', '2018-09-30', '2018-10-31', '2018-11-30', '2018-12-31'],\ 'value': [0.000048, 0.000048, 4.010784, 2.044298, 11.493831, 13.929844, 21.500490, 22.389493, 23.600726, 45.105458, 32.249056, 60.790889, 46.832914],\ 'data_1': [17901701,17901701,17901701,17901701,17901701,17901701,17901701,17901701,17901701,17901701,17901701,17901701,17901701],\ 'data_2': ['3mra','3mra','3mra','3mra','3mra','3mra','3mra','3mra','3mra','3mra','3mra','3mra','3mra'],\ 'data_3': ['Actual','Actual','Actual','Actual','Actual','Actual','Actual','Actual','Actual','Actual','Actual','Actual','Actual'],\ 'data_4': [198.00, 198.00, 0.01, 0.01,0.01,0.01,0.01,0.03,0.03,0.03,0.03,0.03,0.03]}) df.effective_date = pd.to_datetime(df.effective_date) df = df.groupby(['id_1', 'id_2', 'effective_date']).first() Desired outcome The date range I am interested in is 2018-07-31 to 2018-12-31. For each combination of id_1 and id_2, I want to resample on value. For ('ADH10685', 'CA1P0'), I want to get 0 values from September to December. For CB0N0, I want to set September to 0, and for CB0P0, I want to change nothing. value data_1 data_2 data_3 data_4 id_1 id_2 effective_date ADH10685 CA1P0 2018-07-31 0.000048 17901701 3mra Actual 198.00 2018-08-31 0.000048 17901701 3mra Actual 198.00 2018-09-30 0.000000 17901701 3mra Actual 198.00 2018-10-31 0.000000 17901701 3mra Actual 198.00 2018-11-30 0.000000 17901701 3mra Actual 198.00 2018-12-31 0.000000 17901701 3mra Actual 198.00 CB0N0 2018-07-31 4.010784 17901701 3mra Actual 0.01 2018-08-31 2.044298 17901701 3mra Actual 0.01 2018-09-30 0.000008 17901701 3mra Actual 0.01 2018-10-31 11.493831 17901701 3mra Actual 0.01 2018-11-30 13.929844 17901701 3mra Actual 0.01 2018-12-31 21.500490 17901701 3mra Actual 0.01 CB0P0 2018-07-31 22.389493 17901701 3mra Actual 0.03 2018-08-31 23.600726 17901701 3mra Actual 0.03 2018-09-30 45.105458 17901701 3mra Actual 0.03 2018-10-31 32.249056 17901701 3mra Actual 0.03 2018-11-30 60.790889 17901701 3mra Actual 0.03 2018-12-31 46.832914 17901701 3mra Actual 0.03 What I've tried I've asked a couple of questions [1] [2] related to this subject, so I have a sense of how to set the upper and lower limits for the dates and how to resample while keeping the non-value Series intact. I have developed the following code, which works if I hardcode slicing each level. min_date = '2018-07-31' max_date = '2018-12-31' # Slice to specific combination of id_1 and id_2 s = df.loc[('ADD00785', 'CA1P0')] if not s.index.isin([min_date]).any(): s.loc[pd.to_datetime(min_date)] = np.nan if not s.index.isin([max_date]).any(): s.loc[pd.to_datetime(max_date)] = np.nan s.resample('M').first().fillna({'value': 0}).ffill().bfill() I am looking for guidance on is how to best go through a large DataFrame and apply the logic to each pair of (id_1, id_2). I am also looking to clean up my sample code above to be more efficient. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
First, reindex each group of id_1, id_2 by dt. dt = pd.date_range('2018-07-31', '2018-12-31', freq='M') df = (df.reset_index() .groupby(['id_1', 'id_2']) .apply(lambda x: x.set_index('effective_date').reindex(dt)) .drop(columns=['id_1', 'id_2']) .reset_index() .rename(columns={'level_2':'effective_date'})) Then fill missing values in the column value. df['value'] = df['value'].fillna(0) Fill the remaining missing values. df = df.groupby(['id_1', 'id_2']).apply(lambda x: x.ffill(axis=0).bfill(axis=0)) Set id_1, id_2, effective_date back to index. df.set_index(['id_1', 'id_2', 'effective_date'], inplace=True)

Related questions

0 votes
    Hello I am quite new to pygame and I am trying to make an intro for a game where the user hovers ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    I am a bit puzzled by the following code: d = {'x': 1, 'y': 2, 'z': 3} for key in d: print key ... Python? Or is it simply a variable? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    There is a DataFrame from pandas: import pandas as pd inp = [{'e2':20, 'e3':200}, {'e2':22,'e3':220}, { ... '] Can I do this in Pandas? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    Which of the following method is used for transforming a SparseSeries indexed by a MultiIndex to a scipy.sparse ... and answers pdf, Data Science interview questions for beginners...
asked Oct 29, 2021 in Education by JackTerrance
0 votes
    What will be the output of the following Python function? min(max(False,-3,-4), 2,7) a) -4 b) -3 c) 2 d) False...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    I can use vi{ and va{ to select C++ code blocks. It helps me a lot when I need to yank ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Aug 1, 2022 in Education by JackTerrance
0 votes
    I have two list that I combine into a single list. Later I want to make a copy of this list so ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 27, 2022 in Education by JackTerrance
0 votes
    URL is http://*.*.*.*/100/?id=1&version=1 params is {"cityId": "110000", "query": {" ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 27, 2022 in Education by JackTerrance
0 votes
    I have some long data I need to convert into a wide fixed width text format. I have a large ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 11, 2022 in Education by JackTerrance
0 votes
    I have an object with an attribute that is a list. For example: obj.a = [3, 4, 5] I ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    I'm curious about manipulating time in Python. I can get the (last modified) age of a file using ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    I have an object with an attribute that is a list. For example: obj.a = [3, 4, 5] I ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    I want to run a python script from a shell script. I have been able to run it from the "command ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 26, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Pandas Merging 101 (6 answers) Closed 3 years ago. Sorry guys, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 23, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Pandas Merging 101 (6 answers) Closed 3 years ago. Sorry guys, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 14, 2022 in Education by JackTerrance
...