如何将一个值附加到许多名称略有不同的列表中;例如。我有一天中的小时数列表:[1,2,3....,24],我有 24 列(表),如果一天中的小时数等于一个值,则该值应该是一,并且在其他每一列中它应该是 cero`h1 = []h2 = []h3 = []h4 = []h5 = []h6 = []h7 = []h8 = []h9 = []h10 = []h11 = []h12 = []h13 = []h14 = []h15 = []h16 = []h17 = []h18 = []h19 = []h20 = []h21 = []h22 = []h23 = []h24 = []HourTable = []Hour = 0for i in range(0,len(dataset)): Hour = Hora + 1 HourTable.append(Hour) if Hour % 24 == 0: Hour = 0` 基本上我想要类似的东西for h in range(0,len(dataset)):if HourTable[h] == 1: h1.append(1) for hh in range(2,25): ('h'+str(hh)).append(0) #THIS DOES NOT WORK它会将值 0 附加到从 2 到 24 的每个表中。一个人会怎么做呢?谢谢!编辑; 我最终想要完成的是拥有一个包含 24 列的数据库,其中每一列由一个“1”组成,其余列为“0”
2 回答
跃然一笑
TA贡献1826条经验 获得超6个赞
我有点为此管理了一个解决方法,我所做的是;
hours = {}
for i in range(1,25):
hours["h"+str(i)] = []
for n in range(0,len(dataset)):
for qq in range(1,25):
hours['h' + str(qq)].append(0)
hoursDF = pd.DataFrame.from_dict(hours)
for nn in range(0,len(dataset)):
hoursDF['h'+str(HoraTable[nn]%25)][nn] = 1
小唯快跑啊
TA贡献1863条经验 获得超2个赞
只是一个例子,跟进上面的评论:
hours = {}
for i in range(1,25):
hours["h"+str(i)] = []
for h in range(0,len(dataset)):
if HourTable[h] == 1:
hours["h1"].append(1)
for hh in range(2,25):
hours['h'+str(hh)].append(0) # This time it works!
添加回答
举报
0/150
提交
取消