Pandas: Convert a given list of lists into a Dataframe
Pandas: DataFrame Exercise-54 with Solution
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
Python-Pandas Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
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.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/python-exercises/pandas/python-pandas-data-frame-exercise-54.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics