Creating Pandas dataframe using list of lists

Pandas DataFrame is a 2-dimensional labeled data structure with columns of potentially different types. It is generally the most commonly used pandas object.

Pandas DataFrame can be created in multiple ways. Let’s discuss how to create Pandas dataframe using list of lists.

Code #1:



filter_none

edit
close

play_arrow

link
brightness_4
code

# Import pandas library 
import pandas as pd 
  
# initialize list of lists 
data = [['Geeks', 10], ['for', 15], ['geeks', 20]] 
  
# Create the pandas DataFrame 
df = pd.DataFrame(data, columns = ['Name', 'Age']) 
  
# print dataframe. 
print(df )

chevron_right


Output:

Name  Age
0  Geeks   10
1    for   15
2  geeks   20

 
Code #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import pandas library 
import pandas as pd 
  
# initialize list of lists 
data = [['DS', 'Linked_list', 10], ['DS', 'Stack', 9], ['DS', 'Queue', 7],
        ['Algo', 'Greedy', 8], ['Algo', 'DP', 6], ['Algo', 'BackTrack', 5], ] 
  
# Create the pandas DataFrame 
df = pd.DataFrame(data, columns = ['Category', 'Name', 'Marks']) 
  
# print dataframe. 
print(df )

chevron_right


Output:

Category         Name  Marks
0       DS  Linked_list     10
1       DS        Stack      9
2       DS        Queue      7
3     Algo       Greedy      8
4     Algo           DP      6
5     Algo    BackTrack      5

 
Code #3: Doing some operation of dataframe.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import pandas library 
import pandas as pd 
  
# initialize list of lists 
data = [[1, 5, 10], [2, 6, 9], [3, 7, 8]] 
  
# Create the pandas DataFrame 
df = pd.DataFrame(data)
  
# specifying cloumn names
df.columns = ['Col_1', 'Col_2', 'Col_3']
  
# print dataframe. 
print(df, "\n")
  
# transpose of dataframe
df = df.transpose()
print("Transpose of above dataframe is-\n", df)

chevron_right


Output:

Col_1  Col_2  Col_3
0      1      5     10
1      2      6      9
2      3      7      8 

Transpose of above dataframe is-
         0  1  2
Col_1   1  2  3
Col_2   5  6  7
Col_3  10  9  8


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.