Pandas: Convert a given list of lists into a Dataframe
54. Convert List of Lists into DataFrame
Write a Pandas program to convert a given list of lists into a Dataframe.
Sample Solution :
Python Code :
import pandas as pd
my_lists = [['col1', 'col2'], [2, 4], [1, 3]]
# sets the headers as list
headers = my_lists.pop(0)
print("Original list of lists:")
print(my_lists)
df = pd.DataFrame(my_lists, columns = headers)
print("New DataFrame")
print(df)
Sample Output:
Original list of lists: [[2, 4], [1, 3]] New DataFrame col1 col2 0 2 4 1 1 3
For more Practice: Solve these Related Problems:
- Write a Pandas program to convert a nested list into a DataFrame and then assign custom column names.
- Write a Pandas program to transform a list of lists into a DataFrame and then transpose the DataFrame.
- Write a Pandas program to create a DataFrame from a list of lists and then add an index column based on row numbers.
- Write a Pandas program to convert a list of lists into a DataFrame and then filter rows based on a condition applied to one of the columns.
Go to:
Previous: Write a Python Pandas program to convert DataFrame column type from string to datetime.
Next: Write a Pandas program to group by the first column and get second column as lists in rows.
Python-Pandas Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.