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: Convert Date to DateTime
- How to sort a List using Lambda in Python
- Python matplotlib segmentation fault: 11 macOS Big Sur
- What is Terminal Velocity and its Formula? How to calculate it programmatically?
- How to install Python 3.11 on Mac
- How to flatten a nested list in Python
- Python: Pandas Merge DataFrames on Index Example
- How to Run all Cells at Once Jupyter Notebook
- Python - Convert float to String
- How to add borders to tkinter label text
- How to Exit a Loop in Python Code
- [Python] Fix: ValueError: All arrays must be of the same length
- Sorting an array using Bubble Sort in Python Programming
- How to Unzip a file using Python
- Python: Merge DataFrames Pandas Outer Join Example
- Change label (text) color in tkinter
- Convert Float to String in Python
- Fix: fatal error: No such file or directory compilation terminated
- Python: Access index/counter of a for loop iteration
- Import Other Python Files Examples
- How to install Anaconda on Mac (M1/M2 Mac)
- Python Regular Expression to Find All Matches in List
- How to Read a binary File with Python
- How to disable warnings while Python file execution
- Know current Python Version
More Posts:
- How to make a div tag clickable - Html
- What is the Null Object Equivalent in Python? - Python
- How to remove app from Dock when closed [macOS Big Sur] - MacOS
- Microsoft Teams meeting Full Screen mode option missing - bring it back - Teams
- Microsoft Teams Zoom In and Zoom Out Keyboard Shortcut - Teams
- Add two numbers using Java Generics - Java
- WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip. - PIP
- How to Install MongoDB in VSCode - HowTos