#!/usr/bin/env python # coding: utf-8 # # Creating a [`DataFrame`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.html#pandas.DataFrame) # There are actually quite a few ways to create a [`DataFrame`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.html) from existing objects. # # Let's explore! # In[1]: # Setup import pandas as pd # ## From a 2-dimensional object # If your data is already in rows and columns you can just pass it along to the constructor. Labels and Column headings will be automatically generated as a range. # In[2]: test_users_list = [ ['Craig', 'Dennis', 42.42], ['Treasure', 'Porth', 25.00] ] pd.DataFrame(test_users_list) # Notice how both the labels and column headings are autogenerated. You can specify the `index` and `columns`. # In[3]: pd.DataFrame(test_users_list, index=['craigsdennis', 'treasure'], columns=['first_name', 'last_name', 'balance']) # ## From a dictionary # Much like a `Series`, if you do not specify the index it will be autogenerated in range format. # In[4]: # Default expected Dictionary layout is column name, to ordered values test_user_data = { 'first_name': ['Craig', 'Treasure'], 'last_name': ['Dennis', 'Porth'], 'balance': [42.42, 25.00] } pd.DataFrame(test_user_data) # And remember that can specify the index by supplying the `index` keyword argument. # In[5]: pd.DataFrame(test_user_data, index=['craigsdennis', 'treasure']) # ### [`DataFrame.from_dict`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.from_dict.html#pandas.DataFrame.from_dict) adds more options # #### The `orient` keyword # The orient keyword allows you to specify whether the keys of your dictionary are part of the labels (`index`) or the column titles (`columns`). Note how the nested dictionaries have been used to define the columns. You could also pass a list to the `columns` # In[6]: by_username = { 'craigsdennis': { 'first_name': 'Craig', 'last_name': 'Dennis', 'balance': 42.42 }, 'treasure': { 'first_name': 'Treasure', 'last_name': 'Porth', 'balance': 25.00 } } pd.DataFrame.from_dict(by_username, orient='index')