基于使用for循环的条件连接两行

2024-09-30 14:27:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图根据一个条件将字符串连接成两行。我使用for循环来合并值。你知道吗

图像中给出了输入数据集和输出列

enter image description here

我使用for循环来连接行中的值

for i in range(len(Data)):
    j=i+1
    while j < len(Data):
       if(Data['key (Sum(col1to6))'][i]!=Data['key (Sum(col1to6))'][j]):
           break;
    if (Data['key (Sum(col1to6))'][i]==Data['key (Sum(col1to6))'][j]) and     Data['value'][i]<10:
         Data['ouput_code'][i]=Data['Col6'][i]+Data['Col6'][j]
else: 
   Data['ouput_code']=Data['Col6']
   j=j+1
   print ('last',i)

在我的最终输出中,所有行都被合并。你知道吗

enter image description here


Tags: 数据key字符串图像fordatalenif
1条回答
网友
1楼 · 发布于 2024-09-30 14:27:22

虽然我想让你知道,我还没有完全理解你的问题,但看看代码,我看到下面的错误,这可能是造成你的问题。你知道吗

for i in range(len(Data)):
  j=i+1
  while j < len(Data):
    if(Data['key (Sum(col1to6))'][i]!=Data['key (Sum(col1to6))'][j]):
      break;
    if (Data['key (Sum(col1to6))'][i]==Data['key (Sum(col1to6))'][j]) and Data['value'][i]<10:
      Data['ouput_code'][i]=Data['Col6'][i]+Data['Col6'][j]
    else: 
      #Data['ouput_code']=Data['Col6'] # <== Here you are over writing the whole ouput_code column 
      Data['ouput_code'][i]=Data['Col6'][i] # <== I believe what you want to do is update just one entry 
    j=j+1
print ('last',i)

这就是你想做的吗?你知道吗

谢谢

相关问题 更多 >