如何在Python中将列表转换为 DataFrame 行?
在本文中,我们将讨论如何在Python中将列表转换为数据框行。
方法一:使用T函数
这称为转置函数,它将列表转换为一行。这里每个值都存储在一列中。
Syntax: pandas.DataFrame(list).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:从多维列表创建到数据框行
在这里,我们将列表列表转换为数据框行
Syntax: pd.DataFrame(list)
where list is the list of lists
例子:
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:使用带有索引和列的列表
在这里,我们从列表中获取数据(行)并将列分配给列中的这些值
Syntax: pd.DataFrame(list, columns, dtype )
where
- list is the list of input values
- columns are the column names for list of values
- dtype is the column data type
示例:
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函数组合列表。
Syntax pd.DataFrame(list(zip(list1,list2,.,list n)),columns)
where
- columns is the column for the list values
- list1.list n represent number of input lists for columns
示例:
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() 我们可以将列表转换为数据框。
Syntax: pd.DataFrame{‘key’: list1, ‘key’: list2, ……..,’key’: listn}
这些键将是数据框中的列名。
示例:
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()函数中分配列名
Syntax: pd.DataFrame(list,columns)
where
- list is an multidimensional list
- columns are the column names
例子:
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
输出: