How to create a pandas DataFrame using a list?

PandasServer Side ProgrammingProgramming

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.

Example

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

Explanation

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).

Output

     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).

Example

# importing pandas packages
import pandas as pd

L = list('ABCDEFGH')

# creating pandas DataFrame object
df2 = pd.DataFrame(L, columns=['col1'])

print(df2)

Explanation

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.

Output

  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.

raja
Updated on 17-Nov-2021 08:26:28

Advertisements