📌  相关文章
📜  如何在Python中将列表转换为 DataFrame 行?

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

如何在Python中将列表转换为 DataFrame 行?

在本文中,我们将讨论如何在Python中将列表转换为数据框行。

方法一:使用T函数

这称为转置函数,它将列表转换为一行。这里每个值都存储在一列中。

例子:

Python3
# import pandas module
import pandas as pd
 
# consider a list
list1 = ["durga", "ramya", "meghana", "mansa"]
 
# convert the list into dataframe row
data = pd.DataFrame(list1).T
 
# add columns
data.columns = ['student1', 'student2',
                'student3', 'student4']
 
# display
data


Python3
# import pandas module
import pandas as pd
 
# consider a list
list1 = [["durga", "java", 90], ["gopi", "python", 80],
         ["pavani", "c/cpp", 94], ["sravya", "html", 90]]
 
# convert the list into dataframe row
data = pd.DataFrame(list1)
 
# add columns
data.columns = ['student1', 'subject', 'marks']
 
# display
data


Python3
# import pandas module
import pandas as pd
 
# consider a list
list1 = [["durga", "java", 90], ["gopi", "python", 80],
         ["pavani", "c/cpp", 94], ["sravya", "html", 90]]
 
# convert the list into dataframe row by adding columns
data = pd.DataFrame(list1, columns=['student1',
                                    'subject',
                                    'marks'])
 
 
# display
data


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


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 dictionary
dictionary = {'name': list1, 'subject': list2,
              'marks': list3}
 
data = pd.DataFrame(dictionary)
 
# display
data


Python3
# import pandas module
import pandas as pd
 
# consider a list
list1 = [["durga", "java", 90],
         ["gopi", "python", 80],
         ["pavani", "c/cpp", 94],
         ["sravya", "html", 90]]
 
# convert the list into dataframe
# row using columns from multi lists
data = pd.DataFrame(list1, columns=['student1',
                                    'subject',
                                    'marks'])
 
# display
data


输出:

方法2:从多维列表创建到数据框行

在这里,我们将列表列表转换为数据框行

例子:

Python3

# import pandas module
import pandas as pd
 
# consider a list
list1 = [["durga", "java", 90], ["gopi", "python", 80],
         ["pavani", "c/cpp", 94], ["sravya", "html", 90]]
 
# convert the list into dataframe row
data = pd.DataFrame(list1)
 
# add columns
data.columns = ['student1', 'subject', 'marks']
 
# display
data

输出:

方法 3:使用带有索引和列的列表

在这里,我们从列表中获取数据(行)并将列分配给列中的这些值

示例

Python3

# import pandas module
import pandas as pd
 
# consider a list
list1 = [["durga", "java", 90], ["gopi", "python", 80],
         ["pavani", "c/cpp", 94], ["sravya", "html", 90]]
 
# convert the list into dataframe row by adding columns
data = pd.DataFrame(list1, columns=['student1',
                                    'subject',
                                    'marks'])
 
 
# display
data

输出:

方法四:使用 zip()函数

在这里,我们将单独的列表作为输入,这样每个列表将充当一列,因此列表的数量 = 数据框中的 n 列,并使用 zip函数组合列表。

示例

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

输出:

方法5:使用字典列表

在这里,我们将作为数据框中列的单个列表传递给字典的键,因此通过将字典传递给 dataframe() 我们可以将列表转换为数据框。

这些键将是数据框中的列名。

示例

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 dictionary
dictionary = {'name': list1, 'subject': list2,
              'marks': list3}
 
data = pd.DataFrame(dictionary)
 
# display
data

输出:

方法6:从多维列表创建到数据框行与列

在这里,我们从多维列表中获取输入并在 DataFrame()函数中分配列名

例子:

Python3

# import pandas module
import pandas as pd
 
# consider a list
list1 = [["durga", "java", 90],
         ["gopi", "python", 80],
         ["pavani", "c/cpp", 94],
         ["sravya", "html", 90]]
 
# convert the list into dataframe
# row using columns from multi lists
data = pd.DataFrame(list1, columns=['student1',
                                    'subject',
                                    'marks'])
 
# display
data

输出: