create pandas dataframe from lists

How to Create Pandas Dataframe from Lists

Python pandas is a popular library for data analysis and processing. Data is generally loaded into DataFrames which provide tons of functions and methods to simplify data processing. There are several ways to create pandas dataframe in python. In this article, we will learn how to create pandas dataframe from lists in python.


How to Create Pandas Dataframe from Lists

There are many different ways to create pandas dataframe from lists.


1. Basic Example

In this case we simply initialize pandas dataframe constructor with a python list as shown in the following example.

# import pandas as pd
import pandas as pd

# list of strings
lst = ['Hello','World','Now]

# Calling DataFrame constructor on list
df = pd.DataFrame(lst)
df

In this case, the dataframe will consist of 1 column with index of elements starting from 0, 1, 2.

0 Hello
1 World
2 Now


2. Custom Indexes & Column Names

If you want to assign custom index as well as column name by mentioning them as indexes and names arguments in DataFrame() constructor.

# import pandas as pd
import pandas as pd

# list of strings
lst = ['Hello','World','Now']

# Calling DataFrame constructor on list
# with indices and columns specified
df = pd.DataFrame(lst, index =['a', 'b', 'c'],
											columns =['Names'])
df

Now your dataframe will be stored as shown below.

  Names
a Hello
b World
c Now


3. Using Multiple Lists

You can even create DataFrame using multiple lists, by first zipping them together using zip function and passing the result into DataFrame constructor. Here is an example where we create DataFrame using two lists, and also set their column names.

# import pandas as pd
import pandas as pd

# list of strings
lst = ['Hello','World','Now']

# list of int
lst2 = [11, 22, 33]

# Calling DataFrame constructor after zipping
# both lists, with columns specified
df = pd.DataFrame(list(zip(lst, lst2)),
			columns =['Name', 'val'])
df

Now your dataframe will look like the following with column names.

  Name  val
0 Hello 11
1 World 22
2 Now 33


4. Using Multi-Dimensional Lists

You can also use multi-dimensional lists to create DataFrames. Just pass the multi-dimensional lists in DataFrame constructor, as shown in the following example.

# import pandas as pd
import pandas as pd
	
# List1
lst = [['tom', 25], ['krish', 30],
	['nick', 26], ['juli', 22]]
	
df = pd.DataFrame(lst, columns =['Name', 'Age'])
df

In this case, the dataframe will have 2 columns of values.

   Name  Age
0  tom   25
1  krish 30
2  nick  26
3  juli  22

In this article, we have learnt several different ways to create DataFrame from lists. You can use any of them as per your requirement.

Also read:

How to Access Index of Last Element in Pandas DataFrame
MySQL INSERT or UPDATE if Exists
How to Do Regular Expression Replace in MySQL
How to Get Digits from String in MySQL
How to Use SCP with PEM SSH Key File

Leave a Reply

Your email address will not be published. Required fields are marked *