创建Pandas Dataframe的不同方法
Pandas DataFrame是一个二维标注的数据结构,就像任何具有行和列的表格一样。数据框架的大小和值是可变的,也就是说,可以被修改。它是最常用的pandas对象。Pandas DataFrame可以通过多种方式创建。让我们来逐一讨论创建DataFrame的不同方法。
DataFrame()函数用于在Pandas中创建一个数据框架。创建数据框架的语法是。
pandas.DataFrame(data, index, columns)
其中,
data :它是一个数据集,数据框架将从中创建。它可以是列表、字典、标量值、系列、ndarrays等。
index:它是可选的,默认情况下,数据框架的索引从0开始,在最后一个数据值(n-1)结束。它明确地定义了行的标签。
columns :这个参数用于提供数据框架中的列名。如果默认情况下没有定义列名,它将取一个从0到n-1的值。
方法#0:创建一个空的数据框架
# Importing Pandas to create DataFrame
import pandas as pd
# Creating Empty DataFrame and Storing it in variable df
df = pd.DataFrame()
# Printing Empty DataFrame
print(df)
输出:
- pandas的DataFrame()函数被用来创建一个数据框架。
- df变量是我们例子中数据框架的名称。

方法#1:从列表中创建数据框架
# Import pandas library
import pandas as pd
# initialize list elements
data = [10,20,30,40,50,60]
# Create the pandas DataFrame with column name is provided explicitly
df = pd.DataFrame(data, columns=['Numbers'])
# print dataframe.
df
使用列表创建的数据框架
方法#2:从列表的列表中创建Pandas数据框架。
# Import pandas library
import pandas as pd
# initialize list of lists
data = [['tom', 10], ['nick', 15], ['juli', 14]]
# Create the pandas DataFrame
df = pd.DataFrame(data, columns=['Name', 'Age'])
# print dataframe.
df
输出:

方法3:从叙述/列表的dict中创建DataFrame
要从narray/list的dict创建DataFrame,所有的narray必须是相同的长度。如果传递了索引,那么索引的长度应该等于数组的长度。如果没有传递索引,那么默认情况下,索引将是range(n),其中n是数组长度。
# Python code demonstrate creating
# DataFrame from dict narray / lists
# By default addresses.
import pandas as pd
# initialize data of lists.
data = {'Name': ['Tom', 'nick', 'krish', 'jack'],
'Age': [20, 21, 19, 18]}
# Create DataFrame
df = pd.DataFrame(data)
# Print the output.
df
输出:

注意:当使用字典创建数据框架时,字典的键默认为列名。我们也可以使用列参数明确地提供列的名称。
方法#4:通过明确证明索引标签来创建一个数据框架。
# Python code demonstrate creating
# pandas DataFrame with indexed by
# DataFrame using arrays.
import pandas as pd
# initialize data of lists.
data = {'Name': ['Tom', 'Jack', 'nick', 'juli'],
'marks': [99, 98, 95, 90]}
# Creates pandas DataFrame.
df = pd.DataFrame(data, index=['rank1',
'rank2',
'rank3',
'rank4'])
# print the data
df
输出:

方法#5:从数据集列表中创建数据框架
Pandas DataFrame可以通过传递字典列表作为输入数据来创建。默认情况下,字典的键值将被当作列。
# Python code demonstrate how to create
# Pandas DataFrame by lists of dicts.
import pandas as pd
# Initialize data to lists.
data = [{'a': 1, 'b': 2, 'c': 3},
{'a': 10, 'b': 20, 'c': 30}]
# Creates DataFrame.
df = pd.DataFrame(data)
# Print the data
df
输出:

另一个例子是通过传递字典和行索引的列表来创建pandas DataFrame。
# Python code demonstrate to create
# Pandas DataFrame by passing lists of
# Dictionaries and row indices.
import pandas as pd
# Initialize data of lists
data = [{'b': 2, 'c': 3}, {'a': 10, 'b': 20, 'c': 30}]
# Creates pandas DataFrame by passing
# Lists of dictionaries and row index.
df = pd.DataFrame(data, index=['first', 'second'])
# Print the data
df
输出:

另一个例子是,从具有行索引和列索引的字典列表中创建pandas DataFrame。
# Python code demonstrate to create a
# Pandas DataFrame with lists of
# dictionaries as well as
# row and column indexes.
import pandas as pd
# Initialize lists data.
data = [{'a': 1, 'b': 2},
{'a': 5, 'b': 10, 'c': 20}]
# With two column indices, values same
# as dictionary keys
df1 = pd.DataFrame(data, index=['first',
'second'],
columns=['a', 'b'])
# With two column indices with
# one index with other name
df2 = pd.DataFrame(data, index=['first',
'second'],
columns=['a', 'b1'])
# print for first data frame
print(df1, "\n")
# Print for second DataFrame.
print(df2)
输出:

方法#6:使用zip()函数创建DataFrame。
两个列表可以通过使用list(zip())函数进行合并。现在,通过调用pd.DataFrame()函数创建pandas DataFrame。
# Python program to demonstrate creating
# pandas Datadaframe from lists using zip.
import pandas as pd
# List1
Name = ['tom', 'krish', 'nick', 'juli']
# List2
Age = [25, 30, 26, 22]
# get the list of tuples from two lists.
# and merge them by using zip().
list_of_tuples = list(zip(Name, Age))
# Assign data to tuples.
list_of_tuples
# Converting lists of tuples into
# pandas Dataframe.
df = pd.DataFrame(list_of_tuples,
columns=['Name', 'Age'])
# Print data.
df
输出:

方法#7:从系列中创建数据框架。
要从系列中创建一个数据框架,我们必须将系列作为参数传递给DataFrame()函数。
# Python code demonstrate creating
# Pandas Dataframe from series.
import pandas as pd
# Initialize data to series.
d = pd.Series([10, 20, 30, 40])
# creates Dataframe.
df = pd.DataFrame(d)
# print the data.
df
方法#8:从系列字典中创建数据框架。
为了从系列的Dict中创建DataFrame,可以通过dictionary来形成一个DataFrame。结果的索引是所有被传递的系列索引的联合。
# Python code demonstrate creating
# Pandas Dataframe from Dicts of series.
import pandas as pd
# Initialize data to Dicts of series.
d = {'one': pd.Series([10, 20, 30, 40],
index=['a', 'b', 'c', 'd']),
'two': pd.Series([10, 20, 30, 40],
index=['a', 'b', 'c', 'd'])}
# creates Dataframe.
df = pd.DataFrame(d)
# print the data.
df
输出:

极客教程