💡 参考文章见Ref,感谢提供思路!
🗺️当前这篇博文地址:https://myoontyee.github.io/article/bbfbe18f.html
⚠️警告:博客文章禁止一切形式的非授权非法转载!
⚠️Attention: All forms of unauthorized illegal reposts are prohibited !

创建时间:2022年3月23日16:05:04
最新更新:2022年3月30日20:30:09


核心思路

  • 针对的情况是坐标是整数的情况
  • 三个方法
    • 法1:for循环嵌套,按对应索引输入元素
    • 法2:pivot+fillna+reindex+values
    • 法3:set_index+unstack
  • 注意法1需要矩阵Mat初始化,法2、法3不用

输入:DataFrame
输出:Matrix(np.array)


通用

  • 导入库
1
2
3
4
5
6
7
8
9
10
# 基础库
import pandas as pd
import numpy as np

# 进度条
from tqdm import tqdm_notebook

# warnings设置
import warnings
warnings.filterwarnings('ignore')
  • 读一个测试用数据
1
df = pd.read_excel('./meshgridTest.xlsx')
  • 测试用的数据长这样,row代表行索引,col代表列索引,value代表对应索引要填入的元素值
    • 注意Python的索引是从0开始
1
2
3
4
	row	col	value
0 1 1 2
1 2 1 3
2 3 2 1

法1

具体使用

  • 注意法1需要矩阵Mat初始化,法2、法3不用
  • 生成矩阵,并显示进度条
1
2
3
4
5
6
7
8
9
10
11
12
13
14
rows = df['row'].max()+1
cols = df['col'].max()+1

Mat = np.zeros((rows,cols),np.uint8)


for row in tqdm_notebook(range(rows)):
for col in range(0, cols):
value = df[(df['row'] == row) & (df['col'] == col)]['value'].values

if value:
Mat[row, col] = value
else:
pass
1
2
3
4
array([[0, 0, 0],
[0, 2, 0],
[0, 3, 0],
[0, 0, 1]], dtype=uint8)

完整代码块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 基础库
import pandas as pd
import numpy as np

# 进度条
from tqdm import tqdm_notebook

# warnings设置
import warnings
warnings.filterwarnings('ignore')

# ====================================================
# 数据集在这改
# ====================================================
# 读数据集
df = pd.read_excel('./meshgridTest.xlsx')
# ====================================================

# 功能块
rows = df['row'].max()+1
cols = df['col'].max()+1

Mat = np.zeros((rows,cols),np.uint8)

# 运行
for row in tqdm_notebook(range(rows)):
for col in range(0, cols):
value = df[(df['row'] == row) & (df['col'] == col)]['value'].values

if value:
Mat[row, col] = value
else:
pass
  • 输出
1
2
3
4
array([[0, 0, 0],
[0, 2, 0],
[0, 3, 0],
[0, 0, 1]], dtype=uint8)

法2

  • 注意法1需要矩阵Mat初始化,法2、法3不用
  • pivot+fillna+reindex+values

具体使用

1
2
3
4
5
rows = np.arange(df.row.max()+1)
cols = np.arange(df.col.max()+1)

Mat = df.pivot('row', 'col', 'value').fillna(0).reindex(index=rows, columns=cols, fill_value=0).values
print(Mat)
1
2
3
4
[[0. 0. 0.]
[0. 2. 0.]
[0. 3. 0.]
[0. 0. 1.]]

完整代码块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 基础库
import pandas as pd
import numpy as np

# 进度条
from tqdm import tqdm_notebook

# warnings设置
import warnings
warnings.filterwarnings('ignore')

# ====================================================
# 数据集在这改
# ====================================================
# 读数据集
df = pd.read_excel('./meshgridTest.xlsx')
# ====================================================

# 功能块
rows = np.arange(df.row.max()+1)
cols = np.arange(df.col.max()+1)

Mat = df.pivot('row', 'col', 'value').fillna(0).reindex(index=rows, columns=cols, fill_value=0).values

# 打印
print(Mat)
  • 输出
1
2
3
4
[[0. 0. 0.]
[0. 2. 0.]
[0. 3. 0.]
[0. 0. 1.]]

法3

  • 注意法1需要矩阵Mat初始化,法2、法3不用
  • set_index+unstack

具体使用

1
2
3
4
5
rows = np.arange(df.row.max()+1)
cols = np.arange(df.col.max()+1)

Mat = df.set_index(['row', 'col'])['value'].unstack(fill_value=0).reindex(index=rows, columns=cols, fill_value=0).values
print(Mat)
1
2
3
4
array([[0, 0, 0],
[0, 2, 0],
[0, 3, 0],
[0, 0, 1]])

完整代码块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 基础库
import pandas as pd
import numpy as np

# 进度条
from tqdm import tqdm_notebook

# warnings设置
import warnings
warnings.filterwarnings('ignore')

# ====================================================
# 数据集在这改
# ====================================================
# 读数据集
df = pd.read_excel('./meshgridTest.xlsx')
# ====================================================

# 功能块
rows = np.arange(df.row.max()+1)
cols = np.arange(df.col.max()+1)

Mat = df.set_index(['row', 'col'])['value'].unstack(fill_value=0).reindex(index=rows, columns=cols, fill_value=0).values

# 打印
print(Mat)
  • 输出
1
2
3
4
array([[0, 0, 0],
[0, 2, 0],
[0, 3, 0],
[0, 0, 1]])

Ref