HOWTO · Pandas
How to Get List From Pandas DataFrame Series
This article will discuss how to get list from Pandas DataFrame series using tolist method and explore about Pandas DataFrame structure.
On this page
Python is a well-known language for data analysis, mainly due to the Python packages. Pandas is one of those packages that help us analyze data much easier.
Pandas tolist() method converts a series into a series or built-in list of Python. By default, the series is the type of pandas.core.series.Series data type and tolist() method, converted to a list of data.
Use the tolist() Method to Get List From Pandas DataFrame Series
This article will discuss how to get a list from Pandas Dataframe column. We will first read a CSV file into a Pandas DataFrame.
import pandas as pd
# read csv file
df = pd.read_csv("home_price.csv")
# display 3 rows
df = df.head(3)
print(df)
Output:
Area Home price
0 1000 10000
1 1200 12000
2 1300 13000
Now we will extract the value from the column and convert it to the list as we know that tolist() helps.
list1 = df["Home price"].values.tolist()
print("extract the value of series and converting into the list")
print(list1)
Output:
extract the value of series and converting into the list
[10000, 12000, 13000, 14000, 15000]
The list is an ordered and flexible Python container, one of the most common data structures in Python. Elements are inserted into square brackets [], separated by commas to create a list. The list can contain duplicate values; that’s why we mainly use lists in datasets.
import numpy as np
import pandas as pd
# read csv file
df = pd.read_csv("home_price.csv")
# extract the value of series and converting into the list
list1 = df["Home price"].values.tolist()
list1 = np.array(list1)
# type casting in list data type
updated = list(list1 * 1.5)
print("after include 1.5 % tax\n")
print(updated, "new home price")
df["Home price"] = updated
# create new csv
df.to_csv("home prices after 1 year.csv")
df2 = pd.read_csv("home prices after 1 year.csv")
print(df2)
In this case, prices are increased by 1.5 tax in present days. Now we create a list named updated list and update the existing column; further, we create a new CSV file using the to_csv() method.
Output:
after include 1.5 % tax
[15000.0, 18000.0, 19500.0, 21000.0, 22500.0] new home price
Unnamed: 0 Area Home price
0 0 1000 15000.0
1 1 1200 18000.0
2 2 1300 19500.0
3 3 1400 21000.0
4 4 1500 22500.0
Let’s consider another simple example:
import pandas as pd
df = pd.DataFrame(
{
"Country": ["Pakistan", "India", "America", "Russia", "China"],
"Immigrants": ["2000", "2500", "6000", "4000", "1000"],
"Years": ["2010", "2008", "2011", "2018", "2016"],
}
)
print(df, "\n")
list = df.columns.tolist()
print(type(df.columns))
print("\n", list, "\n")
print("After type cast into the list")
print(type(list))
Please observe that the series data type is changed by tolist(), and we got a list with all columns of Dataframe.
Output:
Country Immigrants Years
0 Pakistan 2000 2010
1 India 2500 2008
2 America 6000 2011
3 Russia 4000 2018
4 China 1000 2016
<class 'pandas.core.indexes.base.Index'>
['Country', 'Immigrants', 'Years']
After type cast into the list
<class 'list'>
All the codes are in here.
import numpy as np
import pandas as pd
# read csv file
df = pd.read_csv("home_price.csv")
# display 3 rows
df = df.head(3)
print(df)
list1 = df["Home price"].values.tolist()
print("extract the value of series and converting into the list")
print(list1)
# another example
# read csv file
df = pd.read_csv("home_price.csv")
# extract the value of series and converting into the list
list1 = df["Home price"].values.tolist()
list1 = np.array(list1)
# type casting in list data type
updated = list(list1 * 1.5)
print("after include 1.5 % tax\n")
print(updated, "new home price")
df["Home price"] = updated
# create new csv
df.to_csv("home prices after 1 year.csv")
df2 = pd.read_csv("home prices after 1 year.csv")
print(df2)
# another example
df = pd.DataFrame(
{
"Country": ["Pakistan", "India", "America", "Russia", "China"],
"Immigrants": ["2000", "2500", "6000", "4000", "1000"],
"Years": ["2010", "2008", "2011", "2018", "2016"],
}
)
print(df, "\n")
list = df.columns.tolist()
print(type(df.columns))
print("\n", list, "\n")
print("After type cast into the list")
print(type(list))
Output:
Area Home price
0 1000 10000
1 1200 12000
2 1300 13000
extract the value of series and converting into the list
[10000, 12000, 13000]
after include 1.5 % tax
[15000.0, 18000.0, 19500.0, 21000.0, 22500.0] new home price
Unnamed: 0 Area Home price
0 0 1000 15000.0
1 1 1200 18000.0
2 2 1300 19500.0
3 3 1400 21000.0
4 4 1500 22500.0
Country Immigrants Years
0 Pakistan 2000 2010
1 India 2500 2008
2 America 6000 2011
3 Russia 4000 2018
4 China 1000 2016
<class 'pandas.core.indexes.base.Index'>
['Country', 'Immigrants', 'Years']
After type cast into the list
<class 'list'>