How to use zip() function In Python
Here we are taking separate lists as input such that each list will act as one column, so the number of lists = n columns in the dataframe, and using zip function we are combining the lists.
Syntax pd.DataFrame(list(zip(list1,list2,.,list n)),columns)
where
- columns is the column for the list values
- list1.list n represent number of input lists for columns
Example:
Python3
# import pandas module import pandas as pd # consider a list list1 = [ "durga" , "ramya" , "sravya" ] list2 = [ "java" , "php" , "mysql" ] list3 = [ 67 , 89 , 65 ] # convert the list into dataframe row by # using zip() data = pd.DataFrame( list ( zip (list1, list2, list3)), columns = [ 'student' , 'subject' , 'marks' ]) # display data |
Output:
How to Convert a List to a DataFrame Row in Python?
In this article, we will discuss how to convert a list to a dataframe row in Python.
Contact Us