Search code examples
pythonpandasnumpydataframecomputer-science

How do I normalize my dataframe by inserting new rows with missing values?


I am trying to update my dataframe with values that are missing, the current dataframe looks like this

from pandas import DataFrame
boxes = {
         'A': [0, 4, 10, 15, 30, 50],
         'B': [3, 7, 14, 21, 44, 100],
        }
df = DataFrame(boxes, columns= ['A','B'])

But I need to write a function which can update the dataframe with missing values for column A and B. For example, adding new rows with value A is 8 and B is 9, A is 22, and B is 29 and A is 45 and B is 49


Solution

  • from pandas import DataFrame
    boxes = {'A': [0, 4, 10, 15, 30, 50],
             'B': [3, 7, 14, 21, 44, 100],
            }
    df = DataFrame(boxes, columns= ['A','B'])
    df2 = pd.DataFrame({'A': [8, 22, 45], 'B': [9, 29, 49]}) 
    df.append(df2).reset_index()