Python

Convert List to DataFrame Python

In this tutorial, you will learn about lists and data frames. Also, we discussed different list conversion methods to the data frame in python language. The list in python is the most vital data structure. The important thing about the list is that the list items are not forcibly the same data type, and all the string operations are equally applied on the list data types. Come let’s talk about the data frames.

In python, panda’s library is used for data handling and analysis. Pandas Dataframe is a 2D size-changeable and varied tabular data constructor with marked axes. In Dataframe, knowledge is ranged in a tabular way in columns and rows. Pandas Dataframe contains 3 main essentials, i.e., data, columns, and rows. We will implement our scenarios in Spyder Compiler so let’s get started.

Example 1

We use the basic and simplest approach to convert a list into data frames in our first scenario. To implement your program code, open Spyder IDE from the Windows search bar, then create a new file to write Dataframe creation code into it. After this, start writing your program code. We first import panda’s module and then create a list of strings and add items to it. Then we call the data frame constructor and pass our list as an argument. We can then assign the data frame constructor to a variable.

import pandas as pd

str_list = [‘flower’, ‘tutor’, ‘python’, ‘skills’]
daf = pd.DataFrame(str_list)
print(daf)

After successfully creating your data frame code file, save your file with the “.py” extension. In our scenario, we save our file with “dataframe.py”.

Now run your “dataframe.py” code file and check how you convert the list into a dataframe.

Example 2

We use a Zip() function to convert a list into data frames in our next scenario. We use the same code file for further implementation and write data frame creation code via Zip(). We first import panda’s module and then create a list of strings and add items to it. Here we create two lists. The list of strings and the other one is a list of integers. Then we call the dataframe constructor and pass our list.

We can then assign the data frame constructor to a variable. Then we call the dataframe function and pass two parameters in it. The initial parameter is zip(), and the next is the column. The zip() function takes iterable variables and combines them into a tuple. In the zip function, you can use tuples, sets, lists, or dictionaries. So, the program first zips both files with specified columns and then calls the data frame function.

import pandas as pd

string_list = [‘program’, ‘develop’, ‘coding, ‘skills’]

integer_list = [10, 22, 31, 44]

df = pd.DataFrame(list(zip( string_list, integer_list)), columns = [‘key’, ‘value’])

print(df)

Save and run your “dataframe.py” code file and check how the zip function works:

Example 3

In our third scenario, we use a dictionary to convert a list into data frames. We use the same “dataframe.py” code file and create data frames using lists in the dict. We first import panda’s module and then create a list of strings and add items to it. Here we create three lists. The list of countries, programming languages, and integers. Then we create a dict of lists and assign it to a variable. After that, we call the data frame function, assign it to a variable, and pass dict to it. Then we utilize the print function to show data frames.

import pandas as pd

con_name = [“Japan”, “UK”, “Canada”, “Finland”]
pro_lang = [“Java”, “Python”, “C++”, “.Net]
var_list = [ 11, 44, 33, 55]
dict = { ‘countries’ : con_name, ‘Language’ : pro_lang, ‘numbers’ : var_list
daf = pd.DataFrame(dict)
print(daf)

Again, save and execute the “dataframe.py” code file and check the output display in an ordered way.

Conclusion

If you are working with a large amount of data, it is crucial to first alter the data into a format that a user understands. Data frames provide you with the functionality to efficiently access the data. In python, data is mostly present in the form of a List, and it is significant to create a data frame through a list.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content