从Numpy数组创建一个Pandas DataFrame,并指定索引列和列头
本文演示了多个将Numpy数组转换为Pandas Dataframe并为数据框架指定索引列和列头的例子。
例子1:在这个例子中,Pandas Dataframe将被生成,并且在函数中提到了索引列和列头的正确名称。当索引列和列头的命名没有模式时,可以使用这种方法。
以下是实现。
# Python program to Create a
# Pandas DataFrame from a Numpy
# array and specify the index
# column and column headers
# import required libraries
import numpy as np
import pandas as pd
# creating a numpy array
numpyArray = np.array([[15, 22, 43],
[33, 24, 56]])
# generating the Pandas dataframe
# from the Numpy array and specifying
# name of index and columns
panda_df = pd.DataFrame(data = numpyArray,
index = ["Row_1", "Row_2"],
columns = ["Column_1",
"Column_2", "Column_3"])
# printing the dataframe
print(panda_df)
输出:
例子2:在这个例子中,索引列和列头是通过迭代产生的。行和列的迭代范围是由Numpy数组的形状定义的。每一次迭代,一个数字将被添加到预定义的字符串中,新的索引列或列头将产生。因此,如果在命名数据框架的标签时有一些模式,这种方法是合适的。
以下是实施情况。
# Python program to Create a
# Pandas DataFrame from a Numpy
# array and specify the index column
# and column headers
# import required libraries
import pandas as pd
import numpy as np
# creating a numpy array
numpyArray = np.array([[15, 22, 43],
[33, 24, 56]])
# generating the Pandas dataframe
# from the Numpy array and specifying
# name of index and columns
panda_df = pd.DataFrame(data = numpyArray[0:, 0:],
index = ['Row_' + str(i + 1)
for i in range(numpyArray.shape[0])],
columns = ['Column_' + str(i + 1)
for i in range(numpyArray.shape[1])])
# printing the dataframe
print(panda_df)
输出:
例子3:在这个例子中,在将Numpy数组转换为Pandas数据框架之前,已经定义了索引列和列头。标签名称也是通过迭代产生的,但方法略有不同。在这里,迭代的次数由Numpy数组内的子数组的长度决定。如果索引列和列头名称遵循某种模式,就可以使用这种方法。
以下是实施情况。
# Python program to Create a
# Pandas DataFrame from a Numpy
# array and specify the index column
# and column headers
# import required libraries
import pandas as pd
import numpy as np
# creating a numpy array
numpyArray = np.array([[15, 22, 43],
[33, 24, 56]])
# defining index for the
# Pandas dataframe
index = ['Row_' + str(i)
for i in range(1, len(numpyArray) + 1)]
# defining column headers for the
# Pandas dataframe
columns = ['Column_' + str(i)
for i in range(1, len(numpyArray[0]) + 1)]
# generating the Pandas dataframe
# from the Numpy array and specifying
# details of index and column headers
panda_df = pd.DataFrame(numpyArray ,
index = index,
columns = columns)
# printing the dataframe
print(panda_df)
输出:
例子#4:在这种方法中,Pandas数据框架的索引列和列头将呈现在Numpy数组中。在将Numpy数组转换为Pandas数据框架的过程中,必须对Numpy数组的子数组进行适当的索引,以获得数据框架标签的正确顺序。
以下是实施情况。
# Python program to Create a
# Pandas DataFrame from a Numpy
# array and specify the index column
# and column headers
# import required libraries
import pandas as pd
import numpy as np
# creating a numpy array and
# specifying the index and
# column headers along with
# data stored in the array
numpyArray = np.array([['', 'Column_1',
'Column_2', 'Column_3'],
['Row_1', 15, 22, 43],
['Row_2', 33, 24, 56]])
# generating the Pandas dataframe
# from the Numpy array and specifying
# details of index and column headers
panda_df = pd.DataFrame(data = numpyArray[1:, 1:],
index = numpyArray[1:, 0],
columns = numpyArray[0, 1:])
# printing the dataframe
print(panda_df)
输出: