Appending a single row to a DataFrame

We can append a single row to a DataFrame by passing a series or dictionary to the append method:

    In [152]: 
    algos={'search':['DFS','BFS','Binary Search','Linear'],
            'sorting': ['Quicksort','Mergesort','Heapsort','Bubble Sort'],
           'machine learning':['RandomForest','K Nearest Neighbor','Logistic Regression','K-Means Clustering']}
    algoDF=pd.DataFrame(algos);algoDF
    Out[152]: machine learning    search      sorting
    0    RandomForest        DFS      Quicksort
    1    K Nearest Neighbor   BFS      Mergesort
    2    Logistic Regression  Binary Search Heapsort
    3    K-Means Clustering   Linear       Bubble Sort
    
    In [154]: 
    moreAlgos={'search': 'ShortestPath'  , 'sorting': 'Insertion Sort',
                'machine learning': 'Linear Regression'}
        algoDF.append(moreAlgos,ignore_index=True)

Out[154]: machine learning search sorting 0 RandomForest DFS Quicksort 1 K Nearest Neighbor BFS Mergesort 2 Logistic Regression Binary Search Heapsort 3 K-Means Clustering Linear Bubble Sort 4 Linear Regression ShortestPath Insertion Sort

In order for this to work, you must pass the ignore_index=True argument so that the index [0,1,2,3] in algoDF is ignored.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset