如何将字符串连接为熊猫列表以与附加PYTHON

时间:2018-11-13 16:38:28

标签: python pandas list

如何将值附加到名称稍有不同的许多列表中;例如。 我有一天中的小时列表:[1,2,3 ....,24],我有24列(表),如果一天中的小时等于一个值,则该值应为1 ,并且在其他所有列中都应为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 = 0
for 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

它将2到24的每个表的值附加为0。

一个人怎么做呢?

谢谢!

编辑;最后,我要完成的工作是建立一个包含24列的数据库,其中每列由一个'1'组成,其余各列为'0'

2 个答案:

答案 0 :(得分:0)

我为此设法解决了一个问题,

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    

答案 1 :(得分:-1)

下面的评论只是一个例子:

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!