1. list
list = [[1],
[2],
[3],
[4],
[5]]
print(list)
print(len(list)) # 行数
print(len(list[0])) # 列数
结果:
[[1], [2], [3], [4], [5]]
5
1
2. np.array
初始化和属性
arr1 = np.array([1,2,3,4,5])
print('--------------arr1--------------')
print(arr1.shape)
print(arr1)
arr2 = np.array([[1,2,3,4,5], [13,23,33,43,53]])
print('--------------arr2--------------')
print(arr2.shape)
print(arr2)
arr3 = np.array([[1], [2], [3], [4], [5]])
print('--------------arr3--------------')
print(arr3.shape)
print(arr3)
结果
--------------arr1--------------
(5,)
[1 2 3 4 5]
--------------arr2--------------
(2, 5)
[[ 1 2 3 4 5]
[13 23 33 43 53]]
--------------arr3--------------
(5, 1)
[[1]
[2]
[3]
[4]
[5]]
3. DataFrame
3.1 初始化
# 使用一维数组初始化df,结果df为1列
print('--------------df1--------------')
arr1 = np.array([1, 2, 3, 4, 5])
df1 = pd.DataFrame(arr1)
print(arr1)
print(arr1.shape)
print(df1)
# 使用(5,3)的二维的np.array初始化df,df的列数等于array的列数
print('--------------df2--------------')
df2 = pd.DataFrame(np.ones([5, 3]), columns=['col_1', 'col_2', 'col_3'])
print(df2)
# 用(5,1)的二维数组初始化df
print('--------------df3--------------')
arr3 = np.array([[1], [2], [3]