Improve your Python skills and learn how to create a DataFrame in different ways
DataFrame is a two-dimensional labeled data structures with columns of potentially different types. In general, DataFrame like a spreadsheet and it contains three components: index, columns and data. Dataframes can be created by different ways.
This blog shows you five different ways to create pandas DataFrame. Let?s start?
If you want to create the DataFrame in the image below, you do this using one of the following methods.
1. Create pandas DataFrame from dictionary of lists
The dictionary keys represent the columns names and each list represents a column contents.
# Import pandas libraryimport pandas as pd# Create a dictionary of listdictionary_of_lists = { ‘Name’: [‘Emma’, ‘Oliver’, ‘Harry’, ‘Sophia’], ‘Age’: [29, 25, 33, 24], ‘Department’: [‘HR’, ‘Finance’, ‘Marketing’, ‘IT’]}# Create the DataFramedf1 = pd.DataFrame(dictionary_of_lists)df1
2. Create pandas DataFrame from dictionary of numpy array.
The dictionary keys represent the columns names and each array element represents a column contents.
# Import pandas and numpy librariesimport pandas as pdimport numpy as np# Create a numpy arraynparray = np.array( [[‘Emma’, ‘Oliver’, ‘Harry’, ‘Sophia’], [29, 25, 33, 24], [‘HR’, ‘Finance’, ‘Marketing’, ‘IT’]])# Create a dictionary of nparraydictionary_of_nparray = { ‘Name’: nparray[0], ‘Age’: nparray[1], ‘Department’: nparray[2]}# Create the DataFramedf2 = pd.DataFrame(dictionary_of_nparray)df2
3. Create pandas DataFrame from list of lists
Each inner list represents one row.
# Import pandas libraryimport pandas as pd# Create a list of listslist_of_lists = [ [‘Emma’, 29, ‘HR’], [‘Oliver’, 25, ‘Finance’], [‘Harry’, 33, ‘Marketing’], [‘Sophia’, 24, ‘IT’]]# Create the DataFramedf3 = pd.DataFrame(list_of_lists, columns = [‘Name’, ‘Age’, ‘Department’])df3
4. Create pandas DataFrame from list of dictionaries
Each dictionary represents one row and the keys are the columns names.
# Import pandas libraryimport pandas as pd# Create a list of dictionarieslist_of_dictionaries = [ {‘Name’: ‘Emma’, ‘Age’: 29, ‘Department’: ‘HR’}, {‘Name’: ‘Oliver’, ‘Age’: 25, ‘Department’: ‘Finance’}, {‘Name’: ‘Harry’, ‘Age’: 33, ‘Department’: ‘Marketing’}, {‘Name’: ‘Sophia’, ‘Age’: 24, ‘Department’: ‘IT’}]# Create the DataFramedf4 = pd.DataFrame(list_of_dictionaries)df4
5. Create pandas Dataframe from dictionary of pandas Series
The dictionary keys represent the columns names and each Series represents a column contents.
# Import pandas libraryimport pandas as pd# Create Seriesseries1 = pd.Series([‘Emma’, ‘Oliver’, ‘Harry’, ‘Sophia’])series2 = pd.Series([29, 25, 33, 24])series3 = pd.Series([‘HR’, ‘Finance’, ‘Marketing’, ‘IT’])# Create a dictionary of Seriesdictionary_of_nparray = {‘Name’: series1, ‘Age’: series2, ‘Department’:series3}# Create the DataFramedf5 = pd.DataFrame(dictionary_of_nparray)df5
I hope you find this blog is useful. Thank you for reading 🙂