我有一个零件清单,每个零件由part_number,宽度和长度组成。我想最终将此列表显示为网格,并使用各种宽度作为列标签,各种长度作为行标签。
width1 width2 width3 width4
len1 no1 no2
len2 no3 no4 no5
len3 no6 no7 no8 no9
请注意,并非所有长度和宽度的部件都可用,并且网格中的某些单元格将为空。
我首先将所有这些数据整理成列表。一个用于列标签,一个用于行标签,另一个用于数据思考,我可以使用panda创建一个DataFrame。
columns = []
rows = []
li = []
for part in part_list:
if part.width not in columns:
columns.append(part.width)
if part.length not in rows:
rows.append(part.length)
li.append([part.width, part.length, part.part_number])
data_dict = {
'part_number': weld_stud.part_number,
'diameter_pitch': weld_stud.thread,
'length': weld_stud.fractional_length
}
grid_data.append(data_dict)
然后,我用熊猫做了:
numpy_array = np.array(li)
df = pd.DataFrame(
data=numpy_array[1:,1:], # values
index=numpy_array[1:,0], # 1st column as index
columns=numpy_array[0,1:] # 1st row as the column names
)
这显然不是在输出我需要的东西,但是我不清楚从这里到哪里。
答案 0 :(得分:1)
我多次对此答案进行了修改,所以我希望这是最后一次
答案由两部分组成:
1)数据准备:我不知道您的数据集,但我会猜到
2)数据显示
1)a)数据准备
我知道一个简单的解决方案,可以解决您的数据准备中的所有问题。但是必须满足一个条件,那就是您的宽度和长度是整数。如果是这样,那么解决方案很容易: 假设最大宽度也是10,最大长度也是10。
您以numpy创建11x11的网格(因为从0到10为11个单元格)
# chararray because you can initialize
# grid with being empty (empty string fields)
grid=np.chararray(shape=[11,11])
for part in part_list:
grid[part.width,part.length] = str(part.part_number)
#If you have 2 parts with same parameter you can sum up string so its flexible
因此,基本上,您可以将数组索引用作实际参数width和lentgh
1)b)数据准备
您的问题一直困扰着我,直到我找到了解决您问题的一般方法为止(即使我仍然不知道您的数据集)。 根据上述示例,如果您的参数采用任何形式(int,float,string),您都可以这样做
# Lets say you have 3 types of widths and lengths and you index them
# Key is your parameter, value is index position in grid
widths = {'7.2mm':0,'9.6mm':1,'11.4mm':2}
lengths = {'2.2mm':0,'4.8mm':1,'16.8mm':2}
header = [h for h in widths] # useless in this example
side = [s for s in lengths ] # but can serve for Data display part
grid=np.chararray(shape=[3,3])
for part in part_list:
index_width = widths[part.width]
index_lentgh = lengths[part.length]
grid[index_width ,index_lentgh] = str(part.part_number)
# This way, you dont need to care about missing part numbers and grid can stay unfilled
2)数据显示
from prettytable import PrettyTable
def table(header,side,data):
t = PrettyTable(['']+header)
for i in range(len(data)):
t.add_row([side[i]]+list(data[i]))
print(t)
header = ['col1','col2','col3']
side = ['row1','row2','row3']
data = np.zeros(shape=[3,3])
>> table(header,side,data)
+------+------+------+------+
| | col1 | col2 | col3 |
+------+------+------+------+
| row1 | 0.0 | 0.0 | 0.0 |
| row2 | 0.0 | 0.0 | 0.0 |
| row3 | 0.0 | 0.0 | 0.0 |
+------+------+------+------+
您可以用任何东西(例如类)喂它
data2 = [['','',PrettyTable],['World','',5],['','Hello','']]
table(header,side,data)
>>> table(header,side,data2)
+------+-------+-------+-----------------------------------+
| | col1 | col2 | col3 |
+------+-------+-------+-----------------------------------+
| row1 | | | <class 'prettytable.PrettyTable'> |
| row2 | World | | 5 |
| row3 | | Hello | |
+------+-------+-------+-----------------------------------+
编辑:基于示例数据,我能够执行所需的操作:
import numpy as np
widths = {'#10-24':0, '#10-32':1, '1/4-20':2, '5/16-18':3, '3/8-16':4, '1/2-13':5, '5/8-11':6, '3/4-10':7} # My data dictionary looks like
lenghts = {'5/8':0,'3/4':1,'7/8':2}
part_list = [{'part_number': 'FTC19-62', 'width': '#10-24', 'length': '5/8'},
{'part_number': 'FTC19-75', 'width': '#10-32', 'length': '3/4'},
{'part_number': 'FTC19-87', 'width': '#10-24', 'length': '7/8'}]
grid=np.chararray(shape=[len(lenghts),len(widths)]).astype('|S8')
grid[:,:] = ''
for part in part_list:
index_width = widths[part['width']]
index_lentgh = lenghts[part['length']]
grid[index_lentgh,index_width] = str(part['part_number'])
header = sorted(widths, key=lambda k: widths[k])
side = sorted(lenghts, key=lambda k: lenghts[k])
from prettytable import PrettyTable
def table(header,side,data):
t = PrettyTable(['']+header)
for i in range(len(data)):
t.add_row([side[i]]+list(data[i]))
print(t)
table(header,side,grid)
+-----+----------+----------+--------+---------+--------+--------+--------+--------+
| | #10-24 | #10-32 | 1/4-20 | 5/16-18 | 3/8-16 | 1/2-13 | 5/8-11 | 3/4-10 |
+-----+----------+----------+--------+---------+--------+--------+--------+--------+
| 5/8 | FTC19-62 | | | | | | | |
| 3/4 | | FTC19-75 | | | | | | |
| 7/8 | FTC19-87 | | | | | | | |
+-----+----------+----------+--------+---------+--------+--------+--------+--------+
>>>