Using a dictionary of ndarrays/lists

In the preceding example, the dictionary consisted of Series as the values in the key-value pair. It is possible to construct a DataFrame with a dictionary of lists instead of a dictionary of Series. Unlike the previous case, the row index will not be defined anywhere in the dictionary. Hence, the row label indices are generated using np.range(n). Therefore, it is crucial in this case for all lists or arrays in the dictionary to be of equal length. If this condition is not met, an error occurs.

The dictionary of lists is defined in the following code:

algos = {'search': ['DFS','BFS','Binary Search',
'Linear','ShortestPath (Djikstra)'],
'sorting': ['Quicksort','Mergesort', 'Heapsort',
'Bubble Sort', 'Insertion Sort'],
'machine learning': ['RandomForest', 'K Nearest Neighbor',
'Logistic Regression', ''K-Means Clustering', 'Linear Regression']}

Now, let's convert this dictionary to a DataFrame and print it:

algoDF = pd.DataFrame(algos)

Take a look at the following output:

Here, the row indices are assigned continuous values from 0 to 4. The row indices can also be given custom values as shown in the following code:

pd.DataFrame(algos,index=['algo_1','algo_2','algo_3','algo_4','algo_5'])

Take a look at the following output:

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

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