![]() |
VOOZH | about |
In this article, we will explore the Creating Pandas data frame using a list of lists. A PandasDataFrame is a versatile 2-dimensional labeled data structure with columns that can contain different data types. It is widely utilized as one of the most common objects in the Pandas library. There are various methods for Creating a Pandas data frame using a list of lists, and we will specifically delve into the approach of utilizing a list of lists for this purpose.
There are various methods to create a Pandas data frame using a list of lists. Here, we are discussing some generally used methods that are following
In this example, we will create a list of lists and then pass it to the Pandas DataFrame function. Also, we will add the parameter of columns which will contain the column names.
Output:
Name Age
0 Geeks 10
1 for 15
2 geeks 20
Let's see another example with the same implementation as above.
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
Below code creates a Pandas DataFrame named df from a list of lists, where missing values represented as None are replaced with NaN. It prints the resulting DataFrame containing information about individuals, including names, ages, and occupations.
Output :
Name Age Occuption
0 Geek1 28.0 Engineer
1 Geek2 NaN Data Scientist
2 Geek3 32.0 NaN
Below code creates a Pandas DataFrame from a list of lists, converting the 'Age' column to numeric format and handling errors, with the result printed. The 'Age' values, initially a mix of numbers and strings, are corrected to numeric format.
Output :
Name Age Occupation
0 Geek1 28.0 Engineer
1 Geek2 25.0 Data Scientist
2 Geek3 NaN Manager
Doing some operations on dataframe like transpose. And also defining the Dataframe without column parameters and using df.columns() for the same.
In this example the below code uses pandas to create a DataFrame from a list of lists, assigns column names ('Col_1', 'Col_2', 'Col_3'), prints the original DataFrame, transposes it, and prints the result. Transposing swaps rows and columns in the DataFrame.
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