Full width home advertisement

Post Page Advertisement [Top]

 

5 Ways to Create Pandas DataFrame in Python

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 library
import pandas as pd
# Create a dictionary of list
dictionary_of_lists = {
'Name': ['Emma', 'Oliver', 'Harry', 'Sophia'],
'Age': [29, 25, 33, 24],
'Department': ['HR', 'Finance', 'Marketing', 'IT']}
# Create the DataFrame
df1 = 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 libraries
import pandas as pd
import numpy as np
# Create a numpy array
nparray = np.array(
[['Emma', 'Oliver', 'Harry', 'Sophia'],
[29, 25, 33, 24],
['HR', 'Finance', 'Marketing', 'IT']])
# Create a dictionary of nparray
dictionary_of_nparray = {
'Name': nparray[0],
'Age': nparray[1],
'Department': nparray[2]}
# Create the DataFrame
df2 = pd.DataFrame(dictionary_of_nparray)
df2

3. Create pandas DataFrame from list of lists

Each inner list represents one row.

# Import pandas library
import pandas as pd
# Create a list of lists
list_of_lists = [
['Emma', 29, 'HR'],
['Oliver', 25, 'Finance'],
['Harry', 33, 'Marketing'],
['Sophia', 24, 'IT']]
# Create the DataFrame
df3 = 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 library
import pandas as pd
# Create a list of dictionaries
list_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 DataFrame
df4 = 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 library
import pandas as pd
# Create Series
series1 = pd.Series(['Emma', 'Oliver', 'Harry', 'Sophia'])
series2 = pd.Series([29, 25, 33, 24])
series3 = pd.Series(['HR', 'Finance', 'Marketing', 'IT'])
# Create a dictionary of Series
dictionary_of_nparray = {'Name': series1, 'Age': series2, 'Department':series3}
# Create the DataFrame
df5 = pd.DataFrame(dictionary_of_nparray)
df5

I hope you find this blog is useful. Thank you for reading :)

No comments:

Post a Comment

Bottom Ad [Post Page]

| Designed by Colorlib