Python: How to create a Panda Data frame from a List


We can create a panda data frame from a list using the DataFrame() function.


The DataFrame() is a constructor function in the panda's library which can be used to create a DataFrame object.

Documentation: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html?highlight=dataframe#pandas.DataFrame


Creating a Dataframe using Lists


Example 1:
import pandas as pd

data = [['USA', 'New York', 8623000],
        ['USA', 'Los Angeles', 3990456],
        ['USA', 'Chicago', 2705994],
        ['China', 'Shanghai', 24183300],
        ['China', 'Beijing', 21707000],
        ['India', 'Mumbai', 18414230]]

# DataFrame from the list
df = pd.DataFrame(data, columns=['Country', 'City', 'Population'])

print(df)
Output:
  Country         City  Population
0     USA     New York     8623000
1     USA  Los Angeles     3990456
2     USA      Chicago     2705994
3   China     Shanghai    24183300
4   China      Beijing    21707000
5   India       Mumbai    18414230


Example 2:
import pandas as pd

data = [['US Treasury', 'USA', 1.8],
        ['German Bund', 'Germany', 0.5],
        ['UK Gilt', 'UK', 1.2],
        ['Japanese Government Bond', 'Japan', 0.1],
        ['Australian Government Bond', 'Australia', 2.3]]

df = pd.DataFrame(data, columns=['Bond', 'Country', 'Rate'])

print(df)
Output:
Python Panda List of Dataframe output Example
-

Facing issues? Have Questions? Post them here! I am happy to answer!


Author Info:

Rakesh (He/Him) is a seasoned developer with over 10 years of experience in web and app development, and a deep knowledge of operating systems. Author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2023 | Privacy Policy | About Us | Contact Us | Sitemap