. Advertisement .
..3..
. Advertisement .
..4..
Converting Pandas DataFrame into a List is a fairly basic operation and familiar to those who often have to work and process data. In this article, we will tutorial to Convert Pandas DataFrame into a List in detail. Let’s begin to explore the key and relevant message below!
How to Convert Pandas DataFrame into a List?
To convert a Pandas DataFrame to a list you will need to use a tool that you may already be familiar with, the “tolist()” function. The general syntax for doing it is:
df.values.tolist()
To help you understand better, we will follow the ways below.
Conversion methods
To make it easier for you to visualize and practice, we will give an example below:
Below is a table of data you need to process:
Product | Quantity |
rose | 1200 |
tulip | 700 |
peony | 500 |
lily | 800 |
We will create a Dataframe, and here is code:
import pandas as pd
# Creating a dictionary to store data
data = {'product':['rose', 'tulip', 'peony', 'lily' ],
'quantity': [1200, 700, 500, 800] }
# Creating DataFrame
df = pd.DataFrame(data)
# Print the dataframe
df
Method 1: Convert all to a list (in rows)
To convert all the data into a list (a row-divided list), we will use a program like the one below:
import pandas as pd
# Creating a dictionary to store data
data = {'product':['rose', 'tulip', 'peony', 'lily' ],
'quantity': [1200, 700, 500, 800] }
# Creating DataFrame
df = pd.DataFrame(data)
# Creating an empty list
res=[]
# Iterating through the columns of
# dataframe
for column in df.columns:
# Storing the rows of a column
# into a temporary list
li = df[column].tolist()
# appending the temporary list
res.append(li)
# Printing the final list
print(res)
Output:
[['rose', 'tulip', 'peony', 'lily'], [1200, 700, 500, 800]]
Method 2: Convert data by column
Alternatively, you can also convert the Pandas Dataframe into a list with the data of the columns in the row.
Here is code sample:
import pandas as pd
data = {'product': ['rose','tulip','peony','lily'],
'price': [1200,700,500,800]
}
df = pd.DataFrame(data)
products_list = df.values.tolist()
print(products_list)
output:
[['rose', 1200], ['tulip', 700], ['peony', 500], ['lily', 800]]
Method 3: Convert data to a list of a specific row/column
For example:
import pandas as pd
data = {'product': ['rose','tulip','peony','lily'],
'price': [1200,700,500,800]
}
df = pd.DataFrame(data)
product = df['product'].values.tolist()
print(product)
Output:
['rose', 'tulip', 'peony', 'lily']
Method 4: Convert data to list including row and column names
For example:
import pandas as pd
# Creating a dictionary to store data
data = {'product':['rose', 'tulip', 'peony', 'lily' ],
'quantity': [1200, 700, 500, 800] }
# Creating DataFrame
df = pd.DataFrame(data)
# Converting dataframe to list
li = [df.columns.values.tolist()] + df.values.tolist()
# Printing list
print(li)
output:
[[‘product’, ‘quantity’], [‘rose’, 1200], [‘tulip’, 700], [‘peony’, 500], [‘lily’, 800]]
Conclusion
We hope you enjoyed our article about discovering the answer for your lesson “Tutorial to Convert Pandas DataFrame into a List”. If you have any questions or concerns, please feel free to leave a comment. We are always excited when our posts can provide useful information!
Leave a comment