- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a pandas DataFrame using a list?
DataFrame is a two-dimensional pandas data structure, and it has heterogeneous tabular data with corresponding labels(Rows and Columns).
In general pandas, DataFrame is used to deal with real-time tabular data such as CSV files, SQL Database, and Excel files. If you want to create a DataFrame there are many ways like: by using a list, Numpy array, or a dictionary.
We can create a DataFrame by using a simple list.
import pandas as pd # importing the pandas package Li = [100,200,300,400, 500] # Assigning the value to list(Li) df = pd.DataFrame(Li) # Creating the DataFrame print(df) # Printing the values of DataFrame
Here we take a 5 elements simple list as Li, and initially imported pandas package As pd. By using DataFrame Constructor we created a single column table represented as shape (5X1).
0 0 100 1 200 2 300 3 400 4 500
In this above output, there is a single column labeled as 0 and there are 5 rows labeled from 0-4 integer values representing elements from a list (Li).
# importing pandas packages import pandas as pd L = list('ABCDEFGH') # creating pandas DataFrame object df2 = pd.DataFrame(L, columns=['col1']) print(df2)
This new pandas DataFrame object was created by the python list, with all string elements. To achieve this initially we created a python list object ‘L’ using list function, and then we created a pandas DataFrame object with list ‘L’. While creating the DataFrame object we mentioned the column name “col1” by explicitly specifying the column parameter.
col1 0 A 1 B 2 C 3 D 4 E 5 F 6 G 7 H dtype: object
The integer values in the 1st column is the index values representation for rows in the DataFrame object and the second column “col1” is having string data with positional index values.
- How to create a pandas DataFrame using a list of lists?
- How to create a pandas DataFrame using a list of tuples?
- How to create a pandas DataFrame using a list of dictionaries?
- How to create a pandas DataFrame using a dictionary?
- How to append a list to a Pandas DataFrame using loc in Python?
- How to append a list to a Pandas DataFrame using append() in Python?
- How to append a list to a Pandas DataFrame using iloc in Python?
- How to create a series from a list using Pandas?
- Python Pandas - Create a Subset DataFrame using Indexing Operator
- How to append a list as a row to a Pandas DataFrame in Python?
- Create a Pivot Table as a DataFrame – Python Pandas
- Python – Create a new column in a Pandas dataframe
- How to get the list of column headers from a Pandas DataFrame?
- Use a list of values to select rows from a Pandas DataFrame
- Create a DataFrame with customized index parameters in Pandas