📜  如何将 Pandas DataFrame 转换为列表?

📅  最后修改于: 2022-05-13 01:54:42.896000             🧑  作者: Mango

如何将 Pandas DataFrame 转换为列表?

让我们讨论如何将 Pandas 数据框转换为 List。首先,让我们创建一个 Basic DataFrame:

Python3
import pandas as pd 
 
# Creating a dictionary to store data
data = {'Name':['Tony', 'Steve', 'Bruce', 'Peter' ],
        'Age': [35, 70, 45, 20] } 
 
# Creating DataFrame 
df = pd.DataFrame(data) 
 
# Print the dataframe
df


Python3
df.values.tolist()


Python3
import pandas as pd 
 
# Creating a dictionary to store data
data = {'Name':['Tony', 'Steve', 'Bruce', 'Peter' ] ,
        'Age': [35, 70, 45, 20] } 
 
# Creating DataFrame 
df = pd.DataFrame(data) 
 
# Converting DataFrame to a list containing
# all the rows of column 'Name'
names = df['Name'].tolist()
 
# Printing the converted list.
print(names)


Python3
import pandas as pd 
 
# Creating a dictionary to store data
data = {'Name':['Tony', 'Steve', 'Bruce', 'Peter' ] ,
        'Age': [35, 70, 45, 20] } 
 
# 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)


Python3
import pandas as pd 
 
# Creating a dictionary to store data
data = {'Name':['Tony', 'Steve', 'Bruce', 'Peter' ] ,
        'Age': [35, 70, 45, 20] } 
 
# Creating DataFrame
df = pd.DataFrame(data) 
 
# Converting dataframe to list
li = df.values.tolist()
 
# Printing list
print(li)


Python3
import pandas as pd 
 
# Creating a dictionary to store data
data = {'Name':['Tony', 'Steve', 'Bruce', 'Peter' ] ,
        'Age': [35, 70, 45, 20] } 
 
# Creating DataFrame
df = pd.DataFrame(data) 
 
# Converting dataframe to list
li = [df.columns.values.tolist()] + df.values.tolist()
 
# Printing list
print(li)


输出 :

有时,您可能需要将 pandas 数据框转换为 List。要完成此任务,可以使用“ tolist() ”函数。下面是使用此函数并将所需的 DataFrame 转换为 List 的基本示例。

Python3

df.values.tolist()

输出 :

[['Tony', 35], ['Steve', 70], ['Bruce', 45], ['Peter', 20]]

在这里,每个内部列表都包含特定行的所有列。

Pandas DataFrame 可以通过多种方式转换为列表。让我们来看看一个一个转换 DataFrame 的不同方法。

方法 #1:将 DataFrame 转换为包含特定列的所有行的 List:

Python3

import pandas as pd 
 
# Creating a dictionary to store data
data = {'Name':['Tony', 'Steve', 'Bruce', 'Peter' ] ,
        'Age': [35, 70, 45, 20] } 
 
# Creating DataFrame 
df = pd.DataFrame(data) 
 
# Converting DataFrame to a list containing
# all the rows of column 'Name'
names = df['Name'].tolist()
 
# Printing the converted list.
print(names)

输出:  

['Tony', 'Steve', 'Bruce', 'Peter']

方法 #2:将 DataFrame 转换为包含所有列的所有行的嵌套列表:

Python3

import pandas as pd 
 
# Creating a dictionary to store data
data = {'Name':['Tony', 'Steve', 'Bruce', 'Peter' ] ,
        'Age': [35, 70, 45, 20] } 
 
# 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)

输出:

[['Tony', 'Steve', 'Bruce', 'Peter'], [35, 70, 45, 20]]

方法 #3:将 DataFrame 转换为包含具有行的所有列的列表的列表。

Python3

import pandas as pd 
 
# Creating a dictionary to store data
data = {'Name':['Tony', 'Steve', 'Bruce', 'Peter' ] ,
        'Age': [35, 70, 45, 20] } 
 
# Creating DataFrame
df = pd.DataFrame(data) 
 
# Converting dataframe to list
li = df.values.tolist()
 
# Printing list
print(li)

输出 :

[['Tony', 35], ['Steve', 70], ['Bruce', 45], ['Peter', 20]]

方法 #4:将 DataFrame 转换为包含列表的列表,该列表包含行的所有列以及列名。

Python3

import pandas as pd 
 
# Creating a dictionary to store data
data = {'Name':['Tony', 'Steve', 'Bruce', 'Peter' ] ,
        'Age': [35, 70, 45, 20] } 
 
# Creating DataFrame
df = pd.DataFrame(data) 
 
# Converting dataframe to list
li = [df.columns.values.tolist()] + df.values.tolist()
 
# Printing list
print(li)

输出: