Create pandas dataframe from lists using zip (original) (raw)
Last Updated : 07 Dec, 2022
One of the way to create Pandas DataFrame is by using zip() function. You can use the lists to create lists of tuples and create a dictionary from it. Then, this dictionary can be used to construct a dataframe. zip() function creates the objects and that can be used to produce single item at a time. This function can create pandas DataFrames by merging two lists. Suppose there are two lists of student data, first list holds the name of student and second list holds the age of student. Then we can have,
Python3
Name
=
[
'tom'
,
'krish'
,
'nick'
,
'juli'
]
Age
=
[
25
,
30
,
26
,
22
]
Above two lists can be merged by using list(zip()) function. Now, create the pandas DataFrame by calling pd.DataFrame() function.
Python3
import
pandas as pd
Name
=
[
'tom'
,
'krish'
,
'nick'
,
'juli'
]
Age
=
[
25
,
30
,
26
,
22
]
list_of_tuples
=
list
(
zip
(Name, Age))
list_of_tuples
Output:
Python3
df
=
pd.DataFrame(list_of_tuples, columns
=
[
'Name'
,
'Age'
])
df
Output:
Similar Reads
- Create a Pandas DataFrame from Lists Converting lists to DataFrames is crucial in data analysis, Pandas enabling you to perform sophisticated data manipulations and analyses with ease. List to Dataframe Example # Simple listdata = [1, 2, 3, 4, 5]# Convert to DataFramedf = pd.DataFrame(data, columns=['Numbers'])Here we will discuss diff 5 min read
- Create pandas dataframe from lists using dictionary Pandas DataFrame is a 2-dimensional labeled data structure like any table with rows and columns. The size and values of the dataframe are mutable, i.e., can be modified. It is the most commonly used pandas object. Creating pandas data-frame from lists using dictionary can be achieved in multiple way 2 min read
- Create a list from rows in Pandas dataframe Python lists are one of the most versatile data structures, offering a range of built-in functions for efficient data manipulation. When working with Pandas, we often need to extract entire rows from a DataFrame and store them in a list for further processing. Unlike columns, which are easily access 5 min read
- Creating Pandas dataframe using list of lists In this article, we will explore the Creating Pandas data frame using a list of lists. A Pandas DataFrame 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 v 4 min read
- PySpark - Create DataFrame from List In this article, we are going to discuss how to create a Pyspark dataframe from a list. To do this first create a list of data and a list of column names. Then pass this zipped data to spark.createDataFrame() method. This method is used to create DataFrame. The data attribute will be the list of da 2 min read
- Create a list from rows in Pandas DataFrame | Set 2 In an earlier post, we had discussed some approaches to extract the rows of the dataframe as a Python's list. In this post, we will see some more methods to achieve that goal. Note : For link to the CSV file used in the code, click here. Solution #1: In order to access the data of each row of the Pa 2 min read
- Create a Pandas DataFrame from List of Dicts Pandas DataFrame is a 2-dimensional labeled data structure with columns of potentially different types. It is generally the most commonly used Pandas object. Pandas DataFrame can be created in multiple ways using Python. Let’s discuss how to create a Pandas DataFrame from the List of Dictionaries. C 3 min read
- Create PySpark DataFrame from list of tuples In this article, we are going to discuss the creation of a Pyspark dataframe from a list of tuples. To do this, we will use the createDataFrame() method from pyspark. This method creates a dataframe from RDD, list or Pandas Dataframe. Here data will be the list of tuples and columns will be a list 2 min read
- How to create Pandas DataFrame from nested XML? In this article, we will learn how to create Pandas DataFrame from nested XML. We will use the xml.etree.ElementTree module, which is a built-in module in Python for parsing or reading information from the XML file. The ElementTree represents the XML document as a tree and the Element represents onl 3 min read
- Creating a Pandas dataframe using list of tuples A Pandas DataFrame is an important data structure used for organizing and analyzing data in Python. Converting a list of tuples into a DataFrame makes it easier to work with data. In this article we'll see ways to create a DataFrame from a list of tuples. 1. Using pd.DataFrame()The simplest method t 2 min read