Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何更容易、更正确地向索引添加附加条件,以使代码更简单、更全面?_Python_Dataframe_Conditional Statements - Fatal编程技术网

Python 如何更容易、更正确地向索引添加附加条件,以使代码更简单、更全面?

Python 如何更容易、更正确地向索引添加附加条件,以使代码更简单、更全面?,python,dataframe,conditional-statements,Python,Dataframe,Conditional Statements,上下文: 我正在尝试从数据集中获取满足以下条件的记录: 如果时间戳在凌晨01:00-05:00之间 如果“威胁”值为“3” 编辑: 显然,我可以通过执行“listCoord.index.hour”来打印所选用户的所有小时数,这些小时数是通过“listCoords”获得的。因为在熊猫中,小时是日期时间的一个属性。我在代码的条件语句中使用了这个:color=np.where((listCoords.index.hour==6)、“red”和“#5DBCD2”)] 在这段代码中,我还想添加一个条件

上下文: 我正在尝试从数据集中获取满足以下条件的记录:

  • 如果时间戳在凌晨01:00-05:00之间
  • 如果“威胁”值为“3”
编辑: 显然,我可以通过执行“listCoord.index.hour”来打印所选用户的所有小时数,这些小时数是通过“listCoords”获得的。因为在熊猫中,小时是日期时间的一个属性。我在代码的条件语句中使用了这个:
color=np.where((listCoords.index.hour==6)、“red”和“#5DBCD2”)]

在这段代码中,我还想添加一个条件:listCoord.index.Thread=='3',这样我的代码就变成:

color=np.where((listCoords.index.hour==6)和(listCoord.index.Thread=='3'),'red','blue')]

但很明显,“线程”只是我的数据集中的一个列名,而不是pandas中的DateTime属性问题:如何将第二个条件添加到同一行?因此,我的代码变得更简单,涵盖范围更广




我的数据集
新建示例数据集points.csv:

日期/时间纬度、经度、威胁
2019-03-23 04:00:00,   -14.809489988279145,  26.191607774273443,    1
2019-03-23 04:00:00,   -14.792921094981814,  26.191716715339687,    2
2019-03-23 04:05:00,   -14.798684405083584,  26.162881454312586,    3
2019-03-23 04:10:00,   -14.80112670820822,   26.173830400821103,    2
已编辑-我的代码

   def update_graph(datePicked, selectedData, selectedLocation):
    zoom = 10.5
    latInitial = -14.873619
    lonInitial = 26.106700
    bearing = 0

    if selectedLocation:
        zoom = 13.0
        latInitial = list_of_fixed_sensors[selectedLocation]["lat"]
        lonInitial = list_of_fixed_sensors[selectedLocation]["lon"]

    date_picked = dt.strptime(datePicked, "%Y-%m-%d")
    monthPicked = date_picked.month - 4
    dayPicked = date_picked.day - 1
    listCoords = getLatLonColor(selectedData, monthPicked, dayPicked)

    # print(listCoords.index.Threat)

return go.Figure(
        data=[
            # Data for all observations based on date and time
            Scattermapbox(
                lat=listCoords["Lat"],
                lon=listCoords["Lon"],
                mode="markers",
                hoverinfo="text + lat + lon",
                text=listCoords.index.hour,
                marker=dict(
                    showscale=True,
                    # the color is decided by the time of detection.
                    # color=np.append(np.insert(listCoords.index.hour, 0, 0), 23),
                    color=np.where((listCoords.index.hour == 6), 'red', 'blue')]
唯一的班轮是

df[(df['threat']==3)和df['Date/Time']。在时间('01:00','05:00')之间]

假设
df['Date/Time'].dtype
datetime
如果不使用
pd.to\u datetime

对于编辑案例,请使用:


color=np。其中((listCoords.index.hour==6)和(listCoord['Thread']='3'),'red','blue')]

为了简化威胁过滤器,您可以执行r=df1[df1['threat']==3]将创建一个名为r的新数据帧,其中df1的值等于3。然后你可以做time\u red=r。between\u time()等等。非常感谢,实际上在过去的一个小时里,我在代码中做了很多更改,现在我没有创建新的列表,而是将用户输入带到一个新的列表“listCoords”中,并根据这个新列表应用条件。你能看一下我的帖子编辑,看看你是否能就编辑后的问题提供一些建议吗?你能更新数据框以包含你添加的内容吗。我想你想要
color=np.where((listCoords.index.hour==6)和(listCoord['Thread']==3')、'red'、'blue')]
它奏效了,伙计!(您有一些拼写错误:'color=np.where((listCoords.index.hour==6)&(listCoord['Thread']==3'),'red','blue')]'更新您的答案,以便我可以选择正确的答案:)