Python|从 narray/lists 的字典创建 DataFrame
众所周知,Pandas 一直是数据分析的绝佳工具。最重要的数据类型之一是数据框。它是一种二维标记数据结构,具有可能不同类型的列。它通常是最常用的 pandas 对象。
Pandas DataFrame 可以通过多种方式创建。让我们讨论如何使用 ndarray(或列表)的字典创建 Pandas 数据框。
让我们通过几个例子来更好地理解它。
代码#1:
Python3
# Python code demonstrate creating
# DataFrame from dict narray / lists
# By default addresses.
import pandas as pd
# initialise data of lists.
data = {'Category':['Array', 'Stack', 'Queue'],
'Marks':[20, 21, 19]}
# Create DataFrame
df = pd.DataFrame(data)
# Print the output.
print(df )
Python3
# Python code demonstrate creating
# DataFrame from dict narray / lists
# By default addresses.
import pandas as pd
# initialise data of lists.
data = {'Category':['Array', 'Stack', 'Queue'],
'Student_1':[20, 21, 19], 'Student_2':[15, 20, 14]}
# Create DataFrame
df = pd.DataFrame(data)
# Print the output.
print(df.transpose())
Python3
# Python code demonstrate creating
# DataFrame from dict narray / lists
# By default addresses.
import pandas as pd
# initialise data of lists.
data = {'Area':['Array', 'Stack', 'Queue'],
'Student_1':[20, 21, 19], 'Student_2':[15, 20, 14]}
# Create DataFrame
df = pd.DataFrame(data, index =['Cat_1', 'Cat_2', 'Cat_3'])
# Print the output.
print(df)
输出:
Category Marks
0 Array 20
1 Stack 21
2 Queue 19
注意:要从 narray/list 的 dict 创建 DataFrame,所有 narray 必须具有相同的长度。如果传递了索引,则长度索引应等于数组的长度。如果没有传递索引,则默认情况下,索引将是 range(n),其中 n 是数组长度。代码#2:
Python3
# Python code demonstrate creating
# DataFrame from dict narray / lists
# By default addresses.
import pandas as pd
# initialise data of lists.
data = {'Category':['Array', 'Stack', 'Queue'],
'Student_1':[20, 21, 19], 'Student_2':[15, 20, 14]}
# Create DataFrame
df = pd.DataFrame(data)
# Print the output.
print(df.transpose())
输出:
0 1 2
Category Array Stack Queue
Student_1 20 21 19
Student_2 15 20 14
代码#3:为数据框提供索引列表
Python3
# Python code demonstrate creating
# DataFrame from dict narray / lists
# By default addresses.
import pandas as pd
# initialise data of lists.
data = {'Area':['Array', 'Stack', 'Queue'],
'Student_1':[20, 21, 19], 'Student_2':[15, 20, 14]}
# Create DataFrame
df = pd.DataFrame(data, index =['Cat_1', 'Cat_2', 'Cat_3'])
# Print the output.
print(df)
输出:
Area Student_1 Student_2
Cat_1 Array 20 15
Cat_2 Stack 21 20
Cat_3 Queue 19 14