如何在python中编辑函数,使输出数据框显示N / A?

时间:2018-06-28 18:44:22

标签: python pandas api

enter image description here

我在python下面定义了一个函数,用于根据已经检索到的business_id提取运行时间:

def hours_operation(business_id):
    day = day_open(business_id)
    start = day_start(business_id)
    end = day_end(business_id)
    day1 = []
    for i in day:
        try:
            value = "start"+str(i)
        except:
            value = 'None'
        day1.append(value)
    dict1 = {}
    for i in range(len(day1)):
            dict1[day1[i]]=start[i]
    start_df = pd.DataFrame(dict1, index=[0])    
    day2 = []
    for i in day:
        day2.append("end"+str(i))
    dict2 = {}
    for i in range(len(day2)):
            dict2[day2[i]]=end[i]
    end_df = pd.DataFrame(dict2, index=[0])
    for i in day:
        start_df['end'+str(i)]=end_df['end'+str(i)]
    return start_df

问题是我的输出显示如下:

Out[36]: 
  start0 start1 start2 start3 start4  ...   end2  end3  end4  end5  end6
0   1100   1100   1100   1100   1100  ...   2200  2200  2200  2200  2100

但是,如果企业每周仅工作4天而不是7天,我喜欢这种格式。我希望它以N / A的形式返回开始和结束。

如果一家公司仅工作4天,这就是我期望的输出:

start0 start1 start2 start3 start4 start5 start6 end1   end2  end3  end4  end5  end6
     0   1100   1100   1100   1100   1100    N/A  N/A   2200  2200  2200  N/a   N/A

1 个答案:

答案 0 :(得分:0)

您可以使用python第三级条件语句,如下所示:

version: "3.2"
services:
  superservice:
    image: docker
    command: docker run -it --restart unless-stopped --device=/dev/video0 --volume=/path:/path --name container_name my/image
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    deploy:
      replicas: 1
      mode: replicated
      restart_policy:
        condition: unless-stopped
      resources:
        limits:
          cpus: '0.70'
          memory: 50M
        reservations:
          cpus: '0.25'
          memory: 20M
      placement:
        constraints: [node.role == manager]
    stdin_open: true
    tty: true

这假设dict1[day1[i]]= start[i] if start[i] is not None else 'N/a' 是开始时间的值,并且您不想输出的值是NoneType对象。例如,如果它是0或某个字符串,则只需更新该语句中的条件即可反映出来。 (当然,结束时间您将不得不做同样的事情)