Pandas Create DataFrame from List


Pandas, a powerful data manipulation library in Python, makes it seamless to create DataFrames from various data structures.

In this tutorial, we will learn how to create a Pandas DataFrame from a list, which is a basic and versatile data structure in Python.

    Table of Contents

  1. Creating DataFrame from a List
  2. Creating DataFrame from Multiple Lists
  3. Creating DataFrame from a List of Lists
  4. Creating DataFrame from a List of Dictionaries
  5. Creating DataFrame from a Dictionary of Lists
  6. Conclusion

1. Creating DataFrame from a List

Generally DataFrame is created by importing data from a CSV file or a database table. But sometimes we need to create a DataFrame from a list.

To create a DataFrame from a list, we can use the pd.DataFrame() function and pass the list as an argument to it.

import pandas as pd

list = ['Alice', 'Bob', 'Charlie', 'David']

# ๐Ÿ‘‡ creating DataFrame from list
df = pd.DataFrame(list)
print(df)

Output:

         0
0    Alice
1      Bob
2  Charlie
3    David

Printing the DataFrame we can see that we have two columns, one is the default index column and the other is the column with the list values.


2. Creating DataFrame from Multiple Lists

Now, let's look at how to create a DataFrame from multiple lists. To pass multiple lists to the pd.DataFrame() function, we can use the zip() function to join the lists into a single list of tuples.

import pandas as pd

list1 = ['Alice', 'Bob', 'Charlie', 'David']
list2 = [20, 25, 30, 35]

# ๐Ÿ‘‡ creating DataFrame from multiple lists
df = pd.DataFrame(list(zip(list1, list2)))
print(df)

Output:

         0   1
0    Alice  20
1      Bob  25
2  Charlie  30
3    David  35

Output above shows that each list is mapped to a column in the DataFrame. Each column is labeled with a default index value.

Columns with default index values are not very useful, so it is a good practice to set custom column names.

Setting Custom Column Names

Create a list of column names and pass it as an argument to the columns parameter of the pd.DataFrame() function.

import pandas as pd

list1 = ['Alice', 'Bob', 'Charlie', 'David']
list2 = [20, 25, 30, 35]

# column names
column_names = ['Name', 'Age']

# ๐Ÿ‘‡ creating DataFrame from multiple lists
df = pd.DataFrame(list(zip(list1, list2)), columns=column_names)
print(df)

Output:

      Name  Age
0    Alice   20
1      Bob   25
2  Charlie   30
3    David   35

3. Creating DataFrame from a List of Lists

When we have a list of lists, each inner list is mapped to a row in the DataFrame. So, make sure that inner lists is meant to be a row in the DataFrame.

import pandas as pd

list = [['Alice', 20], ['Bob', 25], ['Charlie', 30], ['David', 35]]
column_names = ['Name', 'Age']

# ๐Ÿ‘‡ creating DataFrame from a list of lists
df = pd.DataFrame(list, columns=column_names)
print(df)

Output:

      Name  Age
0    Alice   20
1      Bob   25
2  Charlie   30
3    David   35

4. Creating DataFrame from a List of Dictionaries

Another way of creating a DataFrame is from a list of dictionaries. Again each dictionary is mapped to a row in the DataFrame.

import pandas as pd

list = [{'Name': 'Alice', 'Age': 20}, {'Name': 'Bob', 'Age': 25}, {'Name': 'Charlie', 'Age': 30}, {'Name': 'David', 'Age': 35}]

# ๐Ÿ‘‡ creating DataFrame from a list of dictionaries
df = pd.DataFrame(list)
print(df)

Output:

      Name  Age
0    Alice   20
1      Bob   25
2  Charlie   30
3    David   35

5. Creating DataFrame from a Dictionary of Lists

Finally, we can have a dictionary of lists to create a DataFrame. Each key in the dictionary is mapped to a column in the DataFrame.

import pandas as pd

dict = {'Name': ['Alice', 'Bob', 'Charlie', 'David'], 'Age': [20, 25, 30, 35]}

# ๐Ÿ‘‡ creating DataFrame from a dictionary of lists
df = pd.DataFrame(dict)
print(df)

Output:

      Name  Age
0    Alice   20
1      Bob   25
2  Charlie   30
3    David   35

Conclusion

Creating a Pandas DataFrame from a list is a fundamental operation when working with tabular data in Python.

Whether you have a list of lists or dictionaries, Pandas offers an easy and efficient way of converting your data into structured DataFrame.