We can create a panda data frame from a list using the DataFrame() function.
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:
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Python,
- Python List of Lists with Examples
- Fix: ERROR: Could not find a version that satisfies the requirement tkinter (from versions: none) ERROR: No matching distribution found for tkinter
- How to Convert Python Notebook .ipynb (JSON) to Executable .py Module
- ModuleNotFoundError: No module named qdarkstyle.colorsystem [Python]
- Python: Pandas Merge Indicator (Left, Right and Both) Example
- Fix: EOFError Exception in Python
- How to URL Decode a Query String in Python
- How to take user input from the console in a Python program
- Compare two lists in Python and return matches
- How to change the Python Default version
- Fix: KeyError: exception in Python
- How to get the Execution Time of A Python Program
- Fix: ModuleNotFoundError: No module named boto3 [Python]
- How to get unique values from a list in Python
- How to pip install Python Modules in VSCode
- Python: Fix - TypeError: NoneType object is not iterable
- How to delete a file using Python code example
- How to create a dictionary comprehension in Python
- Python: Get just the filename without extension using Path
- How to comment out a block of code in Python
- How to add two float numbers in Python
- Python: How to POST Json Data with HTTP Request
- Get MD5 Hash as Checksum of a String in Python
- Python - Convert float to String
- How to Parse XML String in Python
More Posts:
- Convert JSON String to Java GSON Object Example - Java
- What is the Default Admin user and Password for Jenkins - Linux
- Calculate Area of ellipse - C-Program
- How to set background color in HTML page? - Html
- How to install Python 3.9 using brew on Mac - Python
- Java Jackson ObjectMapper Class with Examples - Java
- Vertically Center Text in a DIV Element - CSS
- Unzip a Zip file from Terminal Command - HowTos